feat(auth): 新增手机号与 DCAuth 设备认证登录
- 登录链路改为用户名 + 密码 + 短信验证码 + DCAuth v2 设备签名 - 新增 login_sms_codes / user_devices / device_challenges 三张表,迁移 0063/0064 - 设备绑定只比对 deviceId + publicKey,每账号一个 active 设备,管理员可重置 - 保留未绑定手机号账号的密码登录兜底,绑定后强制安全登录 - 登录页与用户管理页支持验证码、DCAuth 轮询、手机号绑定与设备重置
This commit is contained in:
@@ -1,10 +1,34 @@
|
||||
import api from './client';
|
||||
import type { CurrentUser, LoginResult, MessageResponse } from './types';
|
||||
import type {
|
||||
CurrentUser,
|
||||
DeviceChallengeStatus,
|
||||
DeviceCompletePayload,
|
||||
DevicePreparePayload,
|
||||
DevicePrepareResult,
|
||||
LoginResult,
|
||||
MessageResponse,
|
||||
SmsCodeResult,
|
||||
} from './types';
|
||||
|
||||
export const authApi = {
|
||||
login: (username: string, password: string) =>
|
||||
api.post<LoginResult, LoginResult>('/auth/login', { username, password }),
|
||||
|
||||
sendSmsCode: (username: string, password: string) =>
|
||||
api.post<SmsCodeResult, SmsCodeResult>('/auth/sms-code', { username, password }),
|
||||
|
||||
prepareDeviceLogin: (data: DevicePreparePayload) =>
|
||||
api.post<DevicePrepareResult, DevicePrepareResult>('/auth/prepare', data),
|
||||
|
||||
completeDeviceLogin: (data: DeviceCompletePayload) =>
|
||||
api.post<LoginResult, LoginResult>('/auth/complete', data),
|
||||
|
||||
deviceChallengeStatus: (challengeId: string, pollToken: string) =>
|
||||
api.get<DeviceChallengeStatus, DeviceChallengeStatus>(
|
||||
`/dcauth/challenge/${encodeURIComponent(challengeId)}/status`,
|
||||
{ params: { pollToken } },
|
||||
),
|
||||
|
||||
me: () => api.get<CurrentUser, CurrentUser>('/auth/me'),
|
||||
|
||||
logout: () => api.post<MessageResponse, MessageResponse>('/auth/logout'),
|
||||
|
||||
@@ -10,9 +10,13 @@ const api = axios.create({
|
||||
api.interceptors.response.use(
|
||||
(response) => response.data,
|
||||
(error) => {
|
||||
const requestUrl: string = error.config?.url ?? '';
|
||||
// 登录链路(含 DCAuth 轮询)里的 401 是业务错误,不能当成“登录态失效”跳转。
|
||||
const isAuthFlow =
|
||||
['/auth/login', '/auth/sms-code', '/auth/prepare', '/auth/complete'].includes(requestUrl) ||
|
||||
requestUrl.startsWith('/dcauth/');
|
||||
// 仅在“已登录后 token 失效”时清除登录态并跳转登录页。
|
||||
// 登录接口本身返回 401(用户名/密码错误)时不应跳转,否则会吞掉错误提示。
|
||||
if (error.response?.status === 401 && error.config?.url !== '/auth/login') {
|
||||
if (error.response?.status === 401 && !isAuthFlow) {
|
||||
localStorage.removeItem('user');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
@@ -25,7 +29,10 @@ api.interceptors.response.use(
|
||||
} else if (detail && typeof detail === 'object') {
|
||||
msg = JSON.stringify(detail);
|
||||
}
|
||||
return Promise.reject(new Error(msg));
|
||||
const wrapped = new Error(msg) as Error & { errorCode?: string; status?: number };
|
||||
wrapped.errorCode = error.response?.data?.errorCode;
|
||||
wrapped.status = error.response?.status;
|
||||
return Promise.reject(wrapped);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -272,6 +272,43 @@ export interface CurrentUser {
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export interface SmsCodeResult {
|
||||
sent: boolean;
|
||||
phoneMasked: string;
|
||||
expiresInSeconds: number;
|
||||
provider?: string;
|
||||
}
|
||||
|
||||
export interface DevicePreparePayload {
|
||||
username: string;
|
||||
password: string;
|
||||
smsCode: string;
|
||||
apiBase?: string;
|
||||
}
|
||||
|
||||
export interface DevicePrepareResult {
|
||||
challengeId: string;
|
||||
challenge: string;
|
||||
expiresAt: string;
|
||||
pollToken: string;
|
||||
launchUrl: string;
|
||||
phoneMasked: string;
|
||||
app: string;
|
||||
}
|
||||
|
||||
export interface DeviceChallengeStatus {
|
||||
status: string;
|
||||
deviceTicket?: string;
|
||||
deviceId?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface DeviceCompletePayload {
|
||||
challengeId: string;
|
||||
pollToken: string;
|
||||
deviceTicket: string;
|
||||
}
|
||||
|
||||
export interface UserInfo {
|
||||
id: number;
|
||||
username: string;
|
||||
@@ -280,6 +317,13 @@ export interface UserInfo {
|
||||
remark: string;
|
||||
permissions: string[];
|
||||
custom_permissions?: string[] | null;
|
||||
phone: string;
|
||||
phone_masked: string;
|
||||
phone_bound_at?: string | null;
|
||||
device_id: string;
|
||||
device_id_masked: string;
|
||||
device_bound_at?: string | null;
|
||||
device_last_used_at?: string | null;
|
||||
}
|
||||
|
||||
// ==================== Account ====================
|
||||
|
||||
@@ -10,5 +10,12 @@ export const userApi = {
|
||||
rename: (id: number, username: string) =>
|
||||
api.patch<UserInfo, UserInfo>(`/users/${id}/username`, { username }),
|
||||
delete: (id: number) => api.delete<MessageResponse, MessageResponse>(`/users/${id}`),
|
||||
bindPhone: (id: number, phone: string) =>
|
||||
api.post<UserInfo, UserInfo>(`/admin/users/${id}/phone`, { phone }),
|
||||
resetDevice: (id: number) =>
|
||||
api.post<
|
||||
{ success: boolean; reset: boolean; revoked: number; message: string },
|
||||
{ success: boolean; reset: boolean; revoked: number; message: string }
|
||||
>(`/admin/users/${id}/device/reset`),
|
||||
listPermissions: () => api.get<PermissionsListResult, PermissionsListResult>('/users/permissions/list'),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user