完善登录与状态回显处理
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user