profileShare

rasmusjy / splitapp-frontend-vue

Read-only snapshot

No repository description.

main default branch 96 files Expires Sep 13, 2026, 9:06 AM
AccountService.ts 2,361 bytes
1 import axios from 'axios'
2 import type { IResultObject } from '@/types/IResultObject'
3 import type { IJwtResponse } from '@/types/IJwtResponse'
4
5 const apiClient = axios.create({
6 baseURL: import.meta.env.VITE_API_BASE_URL,
7 })
8
9 export default class AccountService {
10 static async loginAsync(
11 email: string,
12 password: string,
13 ): Promise<IResultObject<IJwtResponse>> {
14 try {
15 const response = await apiClient.post<IJwtResponse>('identity/Account/Login', {
16 email,
17 password,
18 })
19 return { data: response.data }
20 } catch (e) {
21 return { errors: AccountService.handleError(e) }
22 }
23 }
24
25 static async registerAsync(
26 email: string,
27 password: string,
28 firstName: string,
29 lastName: string,
30 ): Promise<IResultObject<IJwtResponse>> {
31 try {
32 const response = await apiClient.post<IJwtResponse>('identity/Account/Register', {
33 email,
34 password,
35 firstName,
36 lastName,
37 })
38 return { data: response.data }
39 } catch (e) {
40 return { errors: AccountService.handleError(e) }
41 }
42 }
43
44 static async refreshTokenAsync(
45 jwt: string,
46 refreshToken: string,
47 ): Promise<IResultObject<IJwtResponse>> {
48 try {
49 const response = await apiClient.post<IJwtResponse>('identity/Account/RefreshTokenData', {
50 jwt,
51 refreshToken,
52 })
53 return { data: response.data }
54 } catch (e) {
55 return { errors: AccountService.handleError(e) }
56 }
57 }
58
59 static async logoutAsync(refreshToken: string): Promise<void> {
60 try {
61 await apiClient.post('identity/Account/Logout', { refreshToken })
62 } catch {
63 // Ignore logout errors — token cleanup happens client-side regardless
64 }
65 }
66
67 private static handleError(e: unknown): string[] {
68 if (axios.isAxiosError(e)) {
69 const data = e.response?.data
70 if (data?.errors && typeof data.errors === 'object') {
71 const messages: string[] = []
72 for (const field of Object.values(data.errors)) {
73 if (Array.isArray(field)) {
74 messages.push(...field)
75 }
76 }
77 if (messages.length > 0) return messages
78 }
79 if (data?.title) {
80 return [data.title]
81 }
82 return [e.response?.statusText ?? 'Unknown error occurred']
83 }
84 return ['Network error or server unavailable']
85 }
86 }
87