完善认证功能,添加注销逻辑并优化用户界面
This commit is contained in:
@@ -4,6 +4,7 @@ import {ApiResponse} from '@/lib/api'
|
|||||||
import {AuthContext} from '@/lib/auth'
|
import {AuthContext} from '@/lib/auth'
|
||||||
import {User} from '@/lib/models'
|
import {User} from '@/lib/models'
|
||||||
import {callByDevice, callByUser, getUserToken} from '@/actions/base'
|
import {callByDevice, callByUser, getUserToken} from '@/actions/base'
|
||||||
|
import {redirect} from 'next/navigation'
|
||||||
|
|
||||||
export interface LoginParams {
|
export interface LoginParams {
|
||||||
username: string
|
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) {
|
export async function getProfile(refresh: boolean = false) {
|
||||||
const cookie = await cookies()
|
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>
|
<span>注册</span>
|
||||||
</Link>
|
</Link>
|
||||||
</>
|
</>
|
||||||
: <>
|
: (
|
||||||
<Link href={`/admin`}>
|
<Link href={`/admin`}>
|
||||||
<Button theme={`gradient`}>
|
<Button theme={`gradient`}>
|
||||||
进入控制台
|
进入控制台
|
||||||
</Button>
|
</Button>
|
||||||
</Link>
|
</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>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {ReactNode} from 'react'
|
|
||||||
import {cookies} from 'next/headers'
|
import {cookies} from 'next/headers'
|
||||||
|
import {Button} from '@/components/ui/button'
|
||||||
|
import {logout} from '@/actions/auth/auth'
|
||||||
|
|
||||||
export type ProfileProps = {}
|
export type ProfileProps = {}
|
||||||
|
|
||||||
@@ -8,8 +9,11 @@ export default async function Profile(props: ProfileProps) {
|
|||||||
const info = store.get('auth_info')?.value
|
const info = store.get('auth_info')?.value
|
||||||
const data = info ? JSON.parse(info) : undefined
|
const data = info ? JSON.parse(info) : undefined
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="flex gap-2 items-center">
|
||||||
下午好,{data?.payload.name}
|
<span>下午好,{data?.payload.name}</span>
|
||||||
|
<Button theme={`error`} onClick={logout}>
|
||||||
|
退出登录
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user