feat: scaffold complete danable frontend architecture with all pages, layouts, stores, and 3 annotator modes
This commit is contained in:
commit
78848dd7d1
173 changed files with 18649 additions and 0 deletions
96
src/services/authService.ts
Normal file
96
src/services/authService.ts
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import { STORAGE_KEYS } from '@/app/constants'
|
||||
import { LoginRequest, LoginResponse, User, UserRole } from '@/types'
|
||||
import { getStorageItem, removeStorageItem, setStorageItem } from '@/utils/storage'
|
||||
import { delay } from './api'
|
||||
import { DEFAULT_USER, getMockDb, saveMockDb } from './mockData'
|
||||
|
||||
export async function login(payload: LoginRequest): Promise<LoginResponse> {
|
||||
await delay(250)
|
||||
const db = getMockDb()
|
||||
const matchedMember = db.members.find(
|
||||
(m) => m.email.toLowerCase() === payload.email.trim().toLowerCase()
|
||||
)
|
||||
|
||||
const user: User = matchedMember
|
||||
? {
|
||||
id: matchedMember.id,
|
||||
name: matchedMember.name,
|
||||
email: matchedMember.email,
|
||||
avatarInitials: matchedMember.initials,
|
||||
avatarColor: matchedMember.avatarColor,
|
||||
role: matchedMember.role,
|
||||
status: matchedMember.status,
|
||||
isOnline: true,
|
||||
lastActive: 'Đang trực tuyến',
|
||||
}
|
||||
: {
|
||||
...DEFAULT_USER,
|
||||
email: payload.email.trim() || DEFAULT_USER.email,
|
||||
}
|
||||
|
||||
db.currentUser = user
|
||||
saveMockDb(db)
|
||||
|
||||
const response: LoginResponse = {
|
||||
user,
|
||||
accessToken: `mock-jwt-token-${user.id}-${Date.now()}`,
|
||||
refreshToken: `mock-refresh-token-${user.id}-${Date.now()}`,
|
||||
}
|
||||
|
||||
setStorageItem(STORAGE_KEYS.ACCESS_TOKEN, response.accessToken)
|
||||
setStorageItem(STORAGE_KEYS.REFRESH_TOKEN, response.refreshToken)
|
||||
setStorageItem(STORAGE_KEYS.CURRENT_USER, user)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export async function loginWithSSO(provider: 'google' | 'microsoft' | 'sso' = 'sso'): Promise<LoginResponse> {
|
||||
await delay(250)
|
||||
const db = getMockDb()
|
||||
const user: User = {
|
||||
...DEFAULT_USER,
|
||||
lastActive: `Đăng nhập qua ${provider.toUpperCase()}`,
|
||||
}
|
||||
db.currentUser = user
|
||||
saveMockDb(db)
|
||||
|
||||
const response: LoginResponse = {
|
||||
user,
|
||||
accessToken: `mock-sso-token-${provider}-${Date.now()}`,
|
||||
refreshToken: `mock-sso-refresh-${provider}-${Date.now()}`,
|
||||
}
|
||||
|
||||
setStorageItem(STORAGE_KEYS.ACCESS_TOKEN, response.accessToken)
|
||||
setStorageItem(STORAGE_KEYS.REFRESH_TOKEN, response.refreshToken)
|
||||
setStorageItem(STORAGE_KEYS.CURRENT_USER, user)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export async function logout(): Promise<void> {
|
||||
await delay(100)
|
||||
removeStorageItem(STORAGE_KEYS.ACCESS_TOKEN)
|
||||
removeStorageItem(STORAGE_KEYS.REFRESH_TOKEN)
|
||||
removeStorageItem(STORAGE_KEYS.CURRENT_USER)
|
||||
}
|
||||
|
||||
export async function getCurrentUser(): Promise<User> {
|
||||
await delay(80)
|
||||
const storedUser = getStorageItem<User | null>(STORAGE_KEYS.CURRENT_USER, null)
|
||||
if (storedUser) return storedUser
|
||||
const db = getMockDb()
|
||||
return db.currentUser || DEFAULT_USER
|
||||
}
|
||||
|
||||
export async function switchUserRole(role: UserRole): Promise<User> {
|
||||
await delay(100)
|
||||
const db = getMockDb()
|
||||
const updated: User = {
|
||||
...db.currentUser,
|
||||
role,
|
||||
}
|
||||
db.currentUser = updated
|
||||
saveMockDb(db)
|
||||
setStorageItem(STORAGE_KEYS.CURRENT_USER, updated)
|
||||
return updated
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue