完善登录与状态回显处理

This commit is contained in:
2025-03-28 15:00:46 +08:00
parent e16ef8e509
commit a16faadaab
17 changed files with 463 additions and 285 deletions

View File

@@ -1,18 +1,22 @@
'use server'
import {cookies} from 'next/headers'
import {ApiResponse, call} from '@/lib/api'
import {AuthContext} from '@/lib/auth'
export interface LoginParams {
username: string;
password: string;
remember?: boolean;
username: string
password: string
remember?: boolean
}
type LoginResp = {
token: string;
expires: number;
token: string
expires: number
auth: AuthContext
}
export async function login(props: LoginParams): Promise<ApiResponse> {
try {
// 尝试登录
@@ -24,9 +28,7 @@ export async function login(props: LoginParams): Promise<ApiResponse> {
if (!result.success) {
return result
}
const data = result.data
console.log('login', data)
// 计算过期时间
const current = Math.floor(Date.now() / 1000)
@@ -40,6 +42,12 @@ export async function login(props: LoginParams): Promise<ApiResponse> {
secure: process.env.NODE_ENV === 'production',
maxAge: Math.max(future, 0),
})
cookieStore.set('auth_info', JSON.stringify(data.auth), {
httpOnly: true,
sameSite: 'strict',
secure: process.env.NODE_ENV === 'production',
maxAge: Math.max(future, 0),
})
return {
success: true,

View File

@@ -6,8 +6,8 @@ import {ApiResponse, call} from '@/lib/api'
export interface VerifyParams {
phone: string;
captcha: string; // 添加验证码字段
phone: string
captcha: string // 添加验证码字段
}
export default async function verify(props: VerifyParams): Promise<ApiResponse> {