feat(web-auth): 前端修改资料、修改密码优化
This commit is contained in:
@@ -1,31 +1,48 @@
|
||||
import type {
|
||||
AuthMeResponse,
|
||||
AuthRefreshResponse,
|
||||
AuthSessionResponse,
|
||||
AuthUser,
|
||||
ChangePasswordRequest,
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
UpdateCurrentUserRequest,
|
||||
} from '@atlas/contracts';
|
||||
import { api } from '@/services/api';
|
||||
import type { MeResult, RefreshResult } from '@/types/auth';
|
||||
|
||||
/** 账户与会话接口,契约见 docs/api/auth.md;类型来自 @atlas/contracts(me/refresh 暂为本地临时类型) */
|
||||
export interface UpdateProfileOptions {
|
||||
/** 提交时的乐观锁版本,取自当前会话 user.revision */
|
||||
revision: number;
|
||||
/** 幂等键,重试安全;成功响应携带新 revision */
|
||||
idempotencyKey: string;
|
||||
}
|
||||
|
||||
/** 账户与会话接口,契约见 docs/api/auth.md;类型全部来自 @atlas/contracts */
|
||||
export const authApi = {
|
||||
register: (payload: RegisterRequest) =>
|
||||
api.post<AuthSessionResponse>('/auth/register', payload),
|
||||
|
||||
login: (payload: LoginRequest) => api.post<AuthSessionResponse>('/auth/login', payload),
|
||||
|
||||
refresh: () => api.post<RefreshResult>('/auth/refresh'),
|
||||
refresh: () => api.post<AuthRefreshResponse>('/auth/refresh'),
|
||||
|
||||
logout: () => api.post<void>('/auth/logout'),
|
||||
|
||||
me: () => api.get<MeResult>('/auth/me'),
|
||||
me: () => api.get<AuthMeResponse>('/auth/me'),
|
||||
|
||||
/**
|
||||
* 【临时 mock】修改资料:PATCH 类接口契约尚未提供(缺口已提报后端 AI)。
|
||||
* 仅在本地模拟成功返回,数据不持久化,刷新即恢复;契约交付后替换为真实调用并删除本实现。
|
||||
* 修改资料:If-Match 乐观锁 + Idempotency-Key 幂等(docs/api/conventions.md §5)。
|
||||
* 版本过期返回 409 VERSION_CONFLICT(details.current_revision 为服务端当前版本)。
|
||||
*/
|
||||
updateProfile: (payload: { display_name: string }): Promise<{ user: Partial<AuthUser> }> =>
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => resolve({ user: { display_name: payload.display_name } }), 300);
|
||||
updateProfile: (payload: UpdateCurrentUserRequest, options: UpdateProfileOptions) =>
|
||||
api.patch<AuthUser>('/auth/me', payload, {
|
||||
headers: {
|
||||
'If-Match': `"${options.revision}"`,
|
||||
'Idempotency-Key': options.idempotencyKey,
|
||||
},
|
||||
}),
|
||||
|
||||
/** 修改密码:成功后所有旧 session 失效;响应携带新 access token,必须立即替换内存值 */
|
||||
changePassword: (payload: ChangePasswordRequest) =>
|
||||
api.post<AuthRefreshResponse>('/auth/change-password', payload),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user