feat(frontend): implement center auth session flow with route guard
This commit is contained in:
40
src/lib/center-auth-client.ts
Normal file
40
src/lib/center-auth-client.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import axios from 'axios'
|
||||
import { getRuntimeConfig } from './runtime-config'
|
||||
import type { AuthSession } from './auth-storage'
|
||||
|
||||
export type LoginPayload = { email: string; password: string }
|
||||
|
||||
type LoginResponse = {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
tokenType: string
|
||||
user: { id: string; email: string }
|
||||
}
|
||||
|
||||
type RefreshResponse = {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
tokenType: string
|
||||
}
|
||||
|
||||
function centerClient() {
|
||||
const cfg = getRuntimeConfig()
|
||||
return axios.create({
|
||||
baseURL: cfg.centerApiBase,
|
||||
timeout: 10000,
|
||||
})
|
||||
}
|
||||
|
||||
export async function loginCenter(payload: LoginPayload): Promise<AuthSession> {
|
||||
const res = await centerClient().post<LoginResponse>('/auth/login', payload)
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function refreshCenter(refreshToken: string): Promise<RefreshResponse> {
|
||||
const res = await centerClient().post<RefreshResponse>('/auth/refresh', { refreshToken })
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function logoutCenter(refreshToken: string): Promise<void> {
|
||||
await centerClient().post('/auth/logout', { refreshToken })
|
||||
}
|
||||
Reference in New Issue
Block a user