김재형

Change login api

import { useState, useCallback } from "react";
import React, { useState, useCallback } from "react";
import ky from "ky";
interface LoginResponse {
status: number;
data: {
access_token: string;
refresh_token: string;
expiration: string;
access: string;
refresh: string;
exp: number;
user: {
int_id: number;
user_id: string;
name: string;
total_size: number;
current_size: number;
root_folder: number;
};
}
......@@ -14,8 +19,18 @@ interface Token {
accessToken: string;
refreshToken: string;
expiration: Date;
user: {
id: number;
username: string;
name: string;
totalSize: number;
currentSize: number;
rootFolder: number;
};
}
export const TokenContext = React.createContext<Token>({} as Token);
export function useAuth() {
const [token, setToken] = useState<Token | null>(() => {
const item = localStorage.getItem("token");
......@@ -29,19 +44,26 @@ export function useAuth() {
const login = useCallback(
async (username: string, password: string, remember: boolean) => {
const body = new URLSearchParams();
body.set("user_id", username);
body.set("password", password);
const response = await ky
.post("/users/login", {
json: {
user_id: username,
password: password,
},
})
.post("/users/login/", { body })
.json<LoginResponse>();
const token = {
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token,
expiration: new Date(response.data.expiration),
accessToken: response.access,
refreshToken: response.refresh,
expiration: new Date(response.exp * 1000),
user: {
id: response.user.int_id,
username: response.user.user_id,
name: response.user.name,
totalSize: response.user.total_size,
currentSize: response.user.current_size,
rootFolder: response.user.root_folder,
},
};
setToken(token);
......