From f5aeaf767df22878c26a3da563d35354f4504457 Mon Sep 17 00:00:00 2001 From: luorijun Date: Fri, 18 Apr 2025 17:48:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=AE=A4=E8=AF=81=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=94=80=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/actions/auth/auth.ts | 43 +++++++++++++++++++ src/app/(api)/alipay/callback/rouute.ts | 5 --- src/app/(api)/wechatpay/callback/rouute.ts | 5 --- .../(root)/@header/_server/user-center.tsx | 15 +------ src/app/admin/_server/profile.tsx | 10 +++-- 5 files changed, 52 insertions(+), 26 deletions(-) delete mode 100644 src/app/(api)/alipay/callback/rouute.ts delete mode 100644 src/app/(api)/wechatpay/callback/rouute.ts diff --git a/src/actions/auth/auth.ts b/src/actions/auth/auth.ts index 084c2ad..a0d81f4 100644 --- a/src/actions/auth/auth.ts +++ b/src/actions/auth/auth.ts @@ -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 { } } +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() diff --git a/src/app/(api)/alipay/callback/rouute.ts b/src/app/(api)/alipay/callback/rouute.ts deleted file mode 100644 index 4f9ab22..0000000 --- a/src/app/(api)/alipay/callback/rouute.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {NextRequest} from 'next/server' - -export async function GET(req: NextRequest) { - -} diff --git a/src/app/(api)/wechatpay/callback/rouute.ts b/src/app/(api)/wechatpay/callback/rouute.ts deleted file mode 100644 index 4f9ab22..0000000 --- a/src/app/(api)/wechatpay/callback/rouute.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {NextRequest} from 'next/server' - -export async function GET(req: NextRequest) { - -} diff --git a/src/app/(root)/@header/_server/user-center.tsx b/src/app/(root)/@header/_server/user-center.tsx index fa0bac4..94ad869 100644 --- a/src/app/(root)/@header/_server/user-center.tsx +++ b/src/app/(root)/@header/_server/user-center.tsx @@ -32,24 +32,13 @@ export default async function UserCenter(props: UserCenterProps) { 注册 - : <> + : ( - {/* profile */} -
- User Avatar -

- {data.payload.name} -

-
- + ) } ) diff --git a/src/app/admin/_server/profile.tsx b/src/app/admin/_server/profile.tsx index bc57d0b..3226159 100644 --- a/src/app/admin/_server/profile.tsx +++ b/src/app/admin/_server/profile.tsx @@ -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 ( -
- 下午好,{data?.payload.name} +
+ 下午好,{data?.payload.name} +
) }