完善认证功能,添加注销逻辑并优化用户界面
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()
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import {NextRequest} from 'next/server'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import {NextRequest} from 'next/server'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
|
||||
}
|
||||
@@ -32,24 +32,13 @@ export default async function UserCenter(props: UserCenterProps) {
|
||||
<span>注册</span>
|
||||
</Link>
|
||||
</>
|
||||
: <>
|
||||
: (
|
||||
<Link href={`/admin`}>
|
||||
<Button theme={`gradient`}>
|
||||
进入控制台
|
||||
</Button>
|
||||
</Link>
|
||||
{/* profile */}
|
||||
<div>
|
||||
<img
|
||||
src={data.payload.avatar}
|
||||
alt="User Avatar"
|
||||
className="w-10 h-10 rounded-full"
|
||||
/>
|
||||
<p>
|
||||
{data.payload.name}
|
||||
</p>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {ReactNode} from 'react'
|
||||
import {cookies} from 'next/headers'
|
||||
import {Button} from '@/components/ui/button'
|
||||
import {logout} from '@/actions/auth/auth'
|
||||
|
||||
export type ProfileProps = {}
|
||||
|
||||
@@ -8,8 +9,11 @@ export default async function Profile(props: ProfileProps) {
|
||||
const info = store.get('auth_info')?.value
|
||||
const data = info ? JSON.parse(info) : undefined
|
||||
return (
|
||||
<div>
|
||||
下午好,{data?.payload.name}
|
||||
<div className="flex gap-2 items-center">
|
||||
<span>下午好,{data?.payload.name}</span>
|
||||
<Button theme={`error`} onClick={logout}>
|
||||
退出登录
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user