完善认证功能,添加注销逻辑并优化用户界面
This commit is contained in:
@@ -4,6 +4,7 @@ import {ApiResponse} from '@/lib/api'
|
||||
import {AuthContext} from '@/lib/auth'
|
||||
import {User} from '@/lib/models'
|
||||
import {callByDevice, callByUser, getUserToken} from '@/actions/base'
|
||||
import {redirect} from 'next/navigation'
|
||||
|
||||
export interface LoginParams {
|
||||
username: string
|
||||
@@ -67,6 +68,48 @@ export async function login(props: LoginParams): Promise<ApiResponse> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const cookieStore = await cookies()
|
||||
|
||||
// 尝试删除后台会话
|
||||
const access_token = cookieStore.get('auth_token')?.value
|
||||
const refresh_token = cookieStore.get('auth_refresh')?.value
|
||||
if (access_token && refresh_token) {
|
||||
await callByDevice('/api/auth/logout', {
|
||||
access_token,
|
||||
refresh_token,
|
||||
})
|
||||
}
|
||||
|
||||
// 删除 cookies
|
||||
cookieStore.set('auth_token', '', {
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: -1,
|
||||
})
|
||||
cookieStore.set('auth_refresh', '', {
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: -1,
|
||||
})
|
||||
cookieStore.set('auth_info', '', {
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: -1,
|
||||
})
|
||||
cookieStore.set('auth_profile', '', {
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: -1,
|
||||
})
|
||||
|
||||
return redirect('/')
|
||||
}
|
||||
|
||||
export async function getProfile(refresh: boolean = false) {
|
||||
const cookie = await cookies()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user