接口调整
This commit is contained in:
parent
9b3c6083f9
commit
3be70555b4
@ -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<Token>('/api/auth/register', data)
|
||||
}
|
||||
|
||||
export async function login(data: LoginInputDto) {
|
||||
return axios.post<Token>('/api/auth/login', data)
|
||||
}
|
||||
|
||||
let refreshing: Promise<AxiosResponse<Token>> | null
|
||||
|
||||
export async function refreshToken(data: TokenRefreshPayload) {
|
||||
if (!refreshing) {
|
||||
refreshing = axios.put<Token>('/api/auth/token', data).finally(() => {
|
||||
refreshing = null
|
||||
})
|
||||
}
|
||||
return refreshing
|
||||
}
|
@ -1,2 +1 @@
|
||||
export * as auth from './auth'
|
||||
export * as user from './user'
|
||||
|
30
src/api/user.interface.ts
Normal file
30
src/api/user.interface.ts
Normal file
@ -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
|
||||
}
|
@ -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<Token>('/api/users', data)
|
||||
}
|
||||
|
||||
export async function login(data: LoginInputDto) {
|
||||
return axios.post<Token>('/api/users/token', data)
|
||||
}
|
||||
|
||||
let refreshing: Promise<AxiosResponse<Token>> | null
|
||||
export async function refreshToken(data: TokenRefreshPayload) {
|
||||
if (!refreshing) {
|
||||
refreshing = axios.put<Token>('/api/users/me/token', data).finally(() => {
|
||||
refreshing = null
|
||||
})
|
||||
}
|
||||
return refreshing
|
||||
}
|
||||
|
||||
export async function getUserInfo() {
|
||||
return axios.get<UserEntity>('/api/users/me')
|
||||
return axios.get<User>('/api/users/me')
|
||||
}
|
||||
|
@ -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('/')
|
||||
|
@ -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('/')
|
||||
|
Loading…
Reference in New Issue
Block a user