2025-04-08 11:21:58 +08:00
|
|
|
'use server'
|
|
|
|
|
import {cookies} from 'next/headers'
|
2025-04-23 19:00:53 +08:00
|
|
|
import {ApiResponse, UnauthorizedError} from '@/lib/api'
|
2025-04-08 11:21:58 +08:00
|
|
|
import {User} from '@/lib/models'
|
2025-04-26 14:18:08 +08:00
|
|
|
import {callByDevice, callByUser} from '@/actions/base'
|
2025-04-08 11:21:58 +08:00
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
type TokenResp = {
|
2025-04-08 11:21:58 +08:00
|
|
|
access_token: string
|
|
|
|
|
refresh_token: string
|
2025-04-23 19:00:53 +08:00
|
|
|
expires_in: number
|
|
|
|
|
token_type: string
|
|
|
|
|
scope?: string
|
2025-04-08 11:21:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
export async function login(props: {
|
|
|
|
|
username: string
|
|
|
|
|
password: string
|
|
|
|
|
remember: boolean
|
|
|
|
|
}): Promise<ApiResponse> {
|
2025-04-08 11:21:58 +08:00
|
|
|
// 尝试登录
|
2025-04-26 14:18:08 +08:00
|
|
|
const result = await callByDevice<TokenResp>('/api/auth/token', {
|
2025-04-23 19:00:53 +08:00
|
|
|
...props,
|
|
|
|
|
grant_type: 'password',
|
|
|
|
|
login_type: 'phone_code',
|
2025-04-08 11:21:58 +08:00
|
|
|
})
|
2025-06-26 10:08:46 +08:00
|
|
|
|
2025-04-08 11:21:58 +08:00
|
|
|
if (!result.success) {
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存到 cookies
|
2025-04-23 19:00:53 +08:00
|
|
|
const data = result.data
|
2025-04-08 11:21:58 +08:00
|
|
|
const cookieStore = await cookies()
|
|
|
|
|
cookieStore.set('auth_token', data.access_token, {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
2025-04-23 19:00:53 +08:00
|
|
|
maxAge: Math.max(data.expires_in, 0),
|
2025-04-08 11:21:58 +08:00
|
|
|
})
|
|
|
|
|
cookieStore.set('auth_refresh', data.refresh_token, {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
2025-06-26 10:08:46 +08:00
|
|
|
maxAge: Number.MAX_SAFE_INTEGER,
|
2025-04-08 11:21:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: undefined,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 17:48:12 +08:00
|
|
|
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) {
|
2025-04-30 16:39:39 +08:00
|
|
|
await callByUser('/api/auth/revoke', {
|
2025-04-18 17:48:12 +08:00
|
|
|
access_token,
|
|
|
|
|
refresh_token,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除 cookies
|
|
|
|
|
cookieStore.set('auth_token', '', {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
maxAge: -1,
|
|
|
|
|
})
|
|
|
|
|
cookieStore.set('auth_refresh', '', {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
maxAge: -1,
|
|
|
|
|
})
|
|
|
|
|
|
2025-04-23 19:00:53 +08:00
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: undefined,
|
|
|
|
|
}
|
2025-04-18 17:48:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 19:00:53 +08:00
|
|
|
export async function getProfile() {
|
2025-04-26 14:18:08 +08:00
|
|
|
return await callByUser<User>('/api/auth/introspect')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function refreshAuth() {
|
|
|
|
|
const cookie = await cookies()
|
|
|
|
|
|
|
|
|
|
const userRefresh = cookie.get('auth_refresh')?.value
|
|
|
|
|
if (!userRefresh) {
|
|
|
|
|
throw UnauthorizedError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 请求刷新访问令牌
|
|
|
|
|
const resp = await callByDevice<TokenResp>(`/api/auth/token`, {
|
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
|
refresh_token: userRefresh,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 处理请求
|
|
|
|
|
if (!resp.success) {
|
2025-06-26 10:08:46 +08:00
|
|
|
if (resp.status === 401) {
|
|
|
|
|
cookie.delete('auth_refresh')
|
|
|
|
|
}
|
2025-04-26 14:18:08 +08:00
|
|
|
throw UnauthorizedError
|
2025-04-08 11:21:58 +08:00
|
|
|
}
|
2025-04-26 14:18:08 +08:00
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
const data = resp.data
|
|
|
|
|
const nextAccessToken = data.access_token
|
|
|
|
|
const nextRefreshToken = data.refresh_token
|
|
|
|
|
const expiresIn = data.expires_in
|
|
|
|
|
|
|
|
|
|
// 保存令牌到 cookies
|
|
|
|
|
cookie.set('auth_token', nextAccessToken, {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
maxAge: Math.max(expiresIn, 0),
|
|
|
|
|
})
|
|
|
|
|
cookie.set('auth_refresh', nextRefreshToken, {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: 'strict',
|
2025-06-27 10:57:50 +08:00
|
|
|
maxAge: Number.MAX_SAFE_INTEGER,
|
2025-04-26 14:18:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 返回新的访问令牌
|
|
|
|
|
return {
|
|
|
|
|
access_token: nextAccessToken,
|
|
|
|
|
refresh_token: nextRefreshToken,
|
2025-04-08 11:21:58 +08:00
|
|
|
}
|
|
|
|
|
}
|