This commit is contained in:
Veljko Tosic
2026-02-15 23:01:27 +01:00
parent 3463ba1e6d
commit 7cd7d4899c
6 changed files with 261 additions and 71 deletions

View File

@@ -1,26 +1,41 @@
import type { AuthResponse, LoginCredentials, SignupCredentials, User } from '@/types'
import { api } from './api'
// TODO: Wire up to real API endpoints via `api` helper
// import { api } from './api'
function normalizeAuthResponse(raw: any): AuthResponse {
const accessToken = raw?.accessToken ?? raw?.AccessToken ?? raw?.token ?? raw?.Token ?? raw?.access_token ?? null
const refreshToken = raw?.refreshToken ?? raw?.RefreshToken ?? raw?.refresh_token ?? null
return { accessToken, refreshToken }
}
export const authService = {
async login(_credentials: LoginCredentials): Promise<AuthResponse> {
// TODO: return api.post<AuthResponse>('/auth/login', credentials)
throw new Error('Not implemented')
async login(credentials: LoginCredentials): Promise<AuthResponse> {
const raw = await api.post<any>('/api/User/login', credentials)
return normalizeAuthResponse(raw)
},
async signup(_credentials: SignupCredentials): Promise<AuthResponse> {
// TODO: return api.post<AuthResponse>('/auth/signup', credentials)
throw new Error('Not implemented')
async refreshLogin(refreshToken: string): Promise<AuthResponse> {
const raw = await api.post<any>('/api/User/refresh-login', { refreshToken })
return normalizeAuthResponse(raw)
},
async logout(): Promise<void> {
// TODO: return api.post<void>('/auth/logout')
throw new Error('Not implemented')
async signup(credentials: SignupCredentials): Promise<AuthResponse> {
const raw = await api.post<any>('/api/User/signup', credentials)
return normalizeAuthResponse(raw)
},
async getCurrentUser(): Promise<User> {
// TODO: return api.get<User>('/auth/me')
throw new Error('Not implemented')
async logout(refreshToken: string): Promise<void> {
return api.delete<void>('/api/User/logout', {refreshToken: refreshToken})
},
async logoutAll(): Promise<void> {
return api.delete<void>('/api/User/logout-all')
},
async getMe() : Promise<User> {
const raw = await api.get<any>('/api/User/me')
// backend User may have fields like userName / UserName and email / Email
const username = raw?.userName ?? raw?.UserName ?? raw?.username ?? raw?.name ?? ''
const email = raw?.email ?? raw?.Email ?? ''
return { username, email }
},
}