-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthService.ts
104 lines (80 loc) · 2.88 KB
/
authService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { User, RegisterData, AuthResponse } from '../models/auth';
import { logApiRequest, logApiResponse, logApiError } from '../utils/logger';
// Login service
export async function login(email: string, password: string): Promise<AuthResponse | null> {
const url = '/api/auth/login';
const method = 'POST';
const body = { email, password };
logApiRequest(method, url, { 'Content-Type': 'application/json' }, body);
const startTime = performance.now();
try {
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const data = await res.json();
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiResponse(method, url, res.status, data, duration);
if (!res.ok) return null;
return data;
} catch (error: unknown) {
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiError(method, url, error, duration);
console.error('Login service error:', error);
return null;
}
}
// Register service
export async function register(userData: RegisterData): Promise<AuthResponse | null> {
const url = '/api/auth/register';
const method = 'POST';
logApiRequest(method, url, { 'Content-Type': 'application/json' }, userData);
const startTime = performance.now();
try {
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData),
});
const data = await res.json();
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiResponse(method, url, res.status, data, duration);
if (!res.ok) return null;
return data;
} catch (error: unknown) {
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiError(method, url, error, duration);
console.error('Registration service error:', error);
return null;
}
}
// Get current user data from API
export async function fetchCurrentUser(token: string): Promise<User | null> {
const url = '/api/auth/me';
const method = 'GET';
const headers = { Authorization: `Bearer ${token}` };
logApiRequest(method, url, headers);
const startTime = performance.now();
try {
const res = await fetch(url, {
headers
});
const data = await res.json();
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiResponse(method, url, res.status, data, duration);
if (!res.ok) return null;
return data;
} catch (error: unknown) {
const endTime = performance.now();
const duration = Math.round(endTime - startTime);
logApiError(method, url, error, duration);
console.error('Fetch user service error:', error);
return null;
}
}