From 3be70555b44663c6bd87cd812aa0aadb598305ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E7=A7=8B=E6=97=AD?= Date: Fri, 24 Feb 2023 09:32:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth.ts | 41 --------------------------------------- src/api/index.ts | 1 - src/api/user.interface.ts | 30 ++++++++++++++++++++++++++++ src/api/user.ts | 35 +++++++++++++++++++++++---------- src/pages/login.tsx | 2 +- src/pages/register.tsx | 2 +- 6 files changed, 57 insertions(+), 54 deletions(-) delete mode 100644 src/api/auth.ts create mode 100644 src/api/user.interface.ts diff --git a/src/api/auth.ts b/src/api/auth.ts deleted file mode 100644 index 57a975c..0000000 --- a/src/api/auth.ts +++ /dev/null @@ -1,41 +0,0 @@ -import axios from '@/utils/axios' -import { type AxiosResponse } from 'axios' - -export interface LoginInputDto { - email: string - password: string -} - -export interface CreateUserDto { - email: string - password: string - username?: string -} - -export interface Token { - accessToken: string - refreshToken: string -} - -export interface TokenRefreshPayload { - refreshToken: string -} - -export async function register(data: CreateUserDto) { - return axios.post('/api/auth/register', data) -} - -export async function login(data: LoginInputDto) { - return axios.post('/api/auth/login', data) -} - -let refreshing: Promise> | null - -export async function refreshToken(data: TokenRefreshPayload) { - if (!refreshing) { - refreshing = axios.put('/api/auth/token', data).finally(() => { - refreshing = null - }) - } - return refreshing -} diff --git a/src/api/index.ts b/src/api/index.ts index 8d10197..0764d36 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,2 +1 @@ -export * as auth from './auth' export * as user from './user' diff --git a/src/api/user.interface.ts b/src/api/user.interface.ts new file mode 100644 index 0000000..9d3f221 --- /dev/null +++ b/src/api/user.interface.ts @@ -0,0 +1,30 @@ +export interface LoginInputDto { + email: string + password: string +} + +export interface CreateUserDto { + email: string + password: string + username?: string +} + +export interface Token { + accessToken: string + refreshToken: string +} + +export interface TokenRefreshPayload { + refreshToken: string +} + +export interface User { + id: string + email: string + username: string | null + password: string + /** @format date-time */ + createdAt: string + /** @format date-time */ + updatedAt: string +} diff --git a/src/api/user.ts b/src/api/user.ts index 43c9f25..c9dc044 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -1,16 +1,31 @@ import axios from '@/utils/axios' +import { type AxiosResponse } from 'axios' +import { + LoginInputDto, + CreateUserDto, + Token, + TokenRefreshPayload, + User, +} from './user.interface' -export interface UserEntity { - id: string - email: string - username: string | null - password: string - /** @format date-time */ - createdAt: string - /** @format date-time */ - updatedAt: string +export async function register(data: CreateUserDto) { + return axios.post('/api/users', data) +} + +export async function login(data: LoginInputDto) { + return axios.post('/api/users/token', data) +} + +let refreshing: Promise> | null +export async function refreshToken(data: TokenRefreshPayload) { + if (!refreshing) { + refreshing = axios.put('/api/users/me/token', data).finally(() => { + refreshing = null + }) + } + return refreshing } export async function getUserInfo() { - return axios.get('/api/users/me') + return axios.get('/api/users/me') } diff --git a/src/pages/login.tsx b/src/pages/login.tsx index 9f05b83..ba7014c 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -26,7 +26,7 @@ export default function Login() { // password: yup.passwordSchema, }), onSubmit: async (values) => { - const res = await api.auth.login(values) + const res = await api.user.login(values) localStorage.setItem('accessToken', res.data.accessToken) localStorage.setItem('refreshToken', res.data.refreshToken) router.push('/') diff --git a/src/pages/register.tsx b/src/pages/register.tsx index 578ffb4..a93cf3b 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -26,7 +26,7 @@ export default function Register() { password: yup.passwordSchema, }), onSubmit: async (values) => { - const res = await api.auth.register(values) + const res = await api.user.register(values) localStorage.setItem('accessToken', res.data.accessToken) localStorage.setItem('refreshToken', res.data.refreshToken) router.push('/')