김재형

Change login api

1 -import { useState, useCallback } from "react"; 1 +import React, { useState, useCallback } from "react";
2 import ky from "ky"; 2 import ky from "ky";
3 3
4 interface LoginResponse { 4 interface LoginResponse {
5 - status: number; 5 + access: string;
6 - data: { 6 + refresh: string;
7 - access_token: string; 7 + exp: number;
8 - refresh_token: string; 8 + user: {
9 - expiration: string; 9 + int_id: number;
10 + user_id: string;
11 + name: string;
12 + total_size: number;
13 + current_size: number;
14 + root_folder: number;
10 }; 15 };
11 } 16 }
12 17
...@@ -14,8 +19,18 @@ interface Token { ...@@ -14,8 +19,18 @@ interface Token {
14 accessToken: string; 19 accessToken: string;
15 refreshToken: string; 20 refreshToken: string;
16 expiration: Date; 21 expiration: Date;
22 + user: {
23 + id: number;
24 + username: string;
25 + name: string;
26 + totalSize: number;
27 + currentSize: number;
28 + rootFolder: number;
29 + };
17 } 30 }
18 31
32 +export const TokenContext = React.createContext<Token>({} as Token);
33 +
19 export function useAuth() { 34 export function useAuth() {
20 const [token, setToken] = useState<Token | null>(() => { 35 const [token, setToken] = useState<Token | null>(() => {
21 const item = localStorage.getItem("token"); 36 const item = localStorage.getItem("token");
...@@ -29,19 +44,26 @@ export function useAuth() { ...@@ -29,19 +44,26 @@ export function useAuth() {
29 44
30 const login = useCallback( 45 const login = useCallback(
31 async (username: string, password: string, remember: boolean) => { 46 async (username: string, password: string, remember: boolean) => {
47 + const body = new URLSearchParams();
48 + body.set("user_id", username);
49 + body.set("password", password);
50 +
32 const response = await ky 51 const response = await ky
33 - .post("/users/login", { 52 + .post("/users/login/", { body })
34 - json: {
35 - user_id: username,
36 - password: password,
37 - },
38 - })
39 .json<LoginResponse>(); 53 .json<LoginResponse>();
40 54
41 const token = { 55 const token = {
42 - accessToken: response.data.access_token, 56 + accessToken: response.access,
43 - refreshToken: response.data.refresh_token, 57 + refreshToken: response.refresh,
44 - expiration: new Date(response.data.expiration), 58 + expiration: new Date(response.exp * 1000),
59 + user: {
60 + id: response.user.int_id,
61 + username: response.user.user_id,
62 + name: response.user.name,
63 + totalSize: response.user.total_size,
64 + currentSize: response.user.current_size,
65 + rootFolder: response.user.root_folder,
66 + },
45 }; 67 };
46 68
47 setToken(token); 69 setToken(token);
......