-
Notifications
You must be signed in to change notification settings - Fork 4
feat(auth): add auth session module with user entity and session management #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import type { AuthTokens, User, UserApis } from '.' | ||
|
|
||
| import { ApiError, createApiClient, getApiAccessToken } from '@/shared/api' | ||
| import type { ApiClient, ApiClientOptions } from '@/shared/api' | ||
|
|
||
| interface BackendUser { | ||
| id: number | ||
| email: string | ||
| nickname: string | null | ||
| email_verified_at: string | null | ||
| status: number | ||
| } | ||
|
|
||
| interface BackendAuthTokens { | ||
| access_token: string | ||
| refresh_token: string | ||
| user: BackendUser | ||
| } | ||
|
|
||
| export interface CreateUserApisOptions extends ApiClientOptions { | ||
| client?: ApiClient | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| function invalidResponse(data: unknown): never { | ||
| throw new ApiError('用户认证响应格式无效', { kind: 'invalid-response', data }) | ||
| } | ||
|
|
||
| function toUser(raw: unknown): User { | ||
| if ( | ||
| !isRecord(raw) || | ||
| typeof raw.id !== 'number' || | ||
| typeof raw.email !== 'string' || | ||
| (typeof raw.nickname !== 'string' && raw.nickname !== null) || | ||
| (typeof raw.email_verified_at !== 'string' && raw.email_verified_at !== null) || | ||
| (raw.status !== 0 && raw.status !== 1) | ||
| ) { | ||
| invalidResponse(raw) | ||
| } | ||
|
|
||
| return { | ||
| id: raw.id, | ||
| email: raw.email, | ||
| nickname: raw.nickname, | ||
| emailVerifiedAt: raw.email_verified_at, | ||
| status: raw.status === 1 ? 'banned' : 'normal', | ||
| } | ||
| } | ||
|
|
||
| function toAuthTokens(raw: unknown): AuthTokens { | ||
| if ( | ||
| !isRecord(raw) || | ||
| typeof raw.access_token !== 'string' || | ||
| typeof raw.refresh_token !== 'string' || | ||
| !isRecord(raw.user) | ||
| ) { | ||
| invalidResponse(raw) | ||
| } | ||
|
|
||
| return { | ||
| accessToken: raw.access_token, | ||
| refreshToken: raw.refresh_token, | ||
| user: toUser(raw.user), | ||
| } | ||
| } | ||
|
|
||
| export function createUserApis(options: CreateUserApisOptions = {}): UserApis { | ||
| const { client, ...clientOptions } = options | ||
| const apiClient = | ||
| client ?? | ||
| createApiClient({ | ||
| ...clientOptions, | ||
| getAccessToken: clientOptions.getAccessToken ?? getApiAccessToken, | ||
| }) | ||
|
|
||
| return { | ||
| async sendCode(input): Promise<void> { | ||
| await apiClient.request<null>('/auth/send-code', { method: 'POST', json: input }) | ||
| }, | ||
|
|
||
| async register(input): Promise<AuthTokens> { | ||
| return toAuthTokens( | ||
| await apiClient.request<BackendAuthTokens>('/auth/register', { | ||
| method: 'POST', | ||
| json: input, | ||
| }), | ||
| ) | ||
| }, | ||
|
|
||
| async login(input): Promise<AuthTokens> { | ||
| return toAuthTokens( | ||
| await apiClient.request<BackendAuthTokens>('/auth/login', { | ||
| method: 'POST', | ||
| json: input, | ||
| }), | ||
| ) | ||
| }, | ||
|
|
||
| async loginByCode(input): Promise<AuthTokens> { | ||
| return toAuthTokens( | ||
| await apiClient.request<BackendAuthTokens>('/auth/login-by-code', { | ||
| method: 'POST', | ||
| json: input, | ||
| }), | ||
| ) | ||
| }, | ||
|
|
||
| async refresh(refreshToken): Promise<AuthTokens> { | ||
| return toAuthTokens( | ||
| await apiClient.request<BackendAuthTokens>('/auth/refresh', { | ||
| method: 'POST', | ||
| json: { refresh_token: refreshToken }, | ||
| }), | ||
| ) | ||
| }, | ||
|
|
||
| async logout(refreshToken): Promise<void> { | ||
| await apiClient.request<null>('/auth/logout', { | ||
| method: 'POST', | ||
| json: { refresh_token: refreshToken }, | ||
| }) | ||
| }, | ||
|
|
||
| async me(): Promise<User> { | ||
| return toUser(await apiClient.request<BackendUser>('/auth/me')) | ||
| }, | ||
|
|
||
| async changePassword(input): Promise<void> { | ||
| await apiClient.request<null>('/auth/change-password', { | ||
| method: 'POST', | ||
| json: { old_password: input.oldPassword, new_password: input.newPassword }, | ||
| }) | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** 已认证用户的前端领域表示。 */ | ||
| export interface User { | ||
| id: number | ||
| email: string | ||
| nickname: string | null | ||
| emailVerifiedAt: string | null | ||
| status: 'normal' | 'banned' | ||
| } | ||
|
|
||
| /** 一次认证成功后由后端签发的访问与刷新令牌。 */ | ||
| export interface AuthTokens { | ||
| accessToken: string | ||
| refreshToken: string | ||
| user: User | ||
| } | ||
|
|
||
| /** 用户认证与账户设置的后端接口。 */ | ||
| export interface UserApis { | ||
| sendCode(input: { | ||
| email: string | ||
| purpose: 'login' | 'register' | 'reset_password' | ||
| }): Promise<void> | ||
| register(input: { | ||
| email: string | ||
| password: string | ||
| code: string | ||
| nickname?: string | ||
| }): Promise<AuthTokens> | ||
| login(input: { email: string; password: string; code: string }): Promise<AuthTokens> | ||
| loginByCode(input: { email: string; code: string }): Promise<AuthTokens> | ||
| refresh(refreshToken: string): Promise<AuthTokens> | ||
| logout(refreshToken: string): Promise<void> | ||
| me(): Promise<User> | ||
| changePassword(input: { oldPassword: string; newPassword: string }): Promise<void> | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里把认证模型定义成了 access/refresh token +
code登录/注册参数,但仓库现有后端UserService仍然是session_token语义,RegisterInput/LoginByPasswordInput也不包含code。按当前代码库状态,这个前端契约不会和后端对得上。