Skip to content

Commit c86ac2a

Browse files
committed
refactor: update AuthService.ts
1 parent b8c06b0 commit c86ac2a

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

src/services/AuthService.ts

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,53 @@ import TokenStorage from '../utils/TokenStorage';
22
import type { AuthConfig, User } from 'react-native-laravel-sanctum';
33

44
class AuthService {
5-
private static config: AuthConfig | null = null;
5+
private readonly config: AuthConfig | null;
66

7-
public constructor(authConfig: AuthConfig) {
8-
if (authConfig === null) throw new Error('AuthConfig is null');
9-
AuthService.config = authConfig;
7+
constructor(authConfig: AuthConfig) {
8+
if (authConfig === null) {
9+
throw new Error('AuthConfig is null');
10+
}
11+
this.config = authConfig;
12+
}
13+
14+
private async handleResponse(response: Response) {
15+
if (!response.ok) {
16+
throw new Error('The request was not successful');
17+
}
1018
}
1119

1220
async login(
1321
email: string,
1422
password: string,
1523
deviceName: string
16-
): Promise<boolean | null> {
24+
): Promise<boolean> {
1725
try {
18-
if (AuthService.config === null) throw new Error('AuthConfig is null');
26+
if (!this.config) {
27+
throw new Error('AuthConfig is null');
28+
}
1929

20-
const response = await fetch(AuthService.config.loginUrl, {
30+
const response = await fetch(this.config.loginUrl, {
2131
method: 'POST',
2232
headers: {
2333
'Content-Type': 'application/json',
2434
},
2535
body: JSON.stringify({
26-
email: email,
27-
password: password,
28-
deviceName: deviceName,
36+
email,
37+
password,
38+
deviceName,
2939
}),
3040
});
3141

32-
if (response.ok) {
33-
const data = await response.json();
34-
const { token } = data;
35-
if (token) {
36-
await TokenStorage.saveToken(token);
37-
return true;
38-
} else {
39-
return false;
40-
}
42+
await this.handleResponse(response);
43+
44+
const data = await response.json();
45+
const { token } = data;
46+
47+
if (token) {
48+
await TokenStorage.saveToken(token);
49+
return true;
4150
} else {
42-
throw new Error('The request was not successful');
51+
return false;
4352
}
4453
} catch (error) {
4554
console.error('Fehler beim Einloggen:', error);
@@ -49,56 +58,61 @@ class AuthService {
4958

5059
async logout(): Promise<boolean> {
5160
try {
52-
if (AuthService.config === null) throw new Error('AuthConfig is null');
61+
if (!this.config) {
62+
throw new Error('AuthConfig is null');
63+
}
64+
5365
const currentToken = await TokenStorage.getToken();
54-
if (currentToken === null) return true;
5566

56-
const response = await fetch(AuthService.config.logoutUrl, {
67+
if (currentToken === null) {
68+
return true;
69+
}
70+
71+
const response = await fetch(this.config.logoutUrl, {
5772
method: 'POST',
5873
headers: {
5974
'Content-Type': 'application/json',
6075
'Authorization': `Bearer ${currentToken}`,
6176
},
6277
});
6378

64-
if (response.ok) {
65-
await TokenStorage.removeToken();
66-
return true;
67-
} else {
68-
throw new Error('The request was not successful');
69-
}
79+
await this.handleResponse(response);
80+
await TokenStorage.removeToken();
81+
return true;
7082
} catch (error) {
71-
console.error('Fehler beim Einloggen:', error);
83+
console.error('Fehler beim Ausloggen:', error);
7284
throw error;
7385
}
7486
}
7587

7688
async getUser(): Promise<User | null> {
77-
if (AuthService.config === null) throw new Error('AuthConfig is null');
78-
const currentToken = await TokenStorage.getToken();
79-
if (currentToken === null) return null;
80-
console.log(`Bearer ${currentToken}`);
8189
try {
82-
const response = await fetch(AuthService.config.userUrl, {
90+
if (!this.config) {
91+
throw new Error('AuthConfig is null');
92+
}
93+
94+
const currentToken = await TokenStorage.getToken();
95+
96+
if (currentToken === null) {
97+
return null;
98+
}
99+
100+
const response = await fetch(this.config.userUrl, {
83101
method: 'GET',
84102
headers: {
85103
'Content-Type': 'application/json',
86104
'Authorization': `Bearer ${currentToken}`,
87105
},
88106
});
89107

90-
if (response.ok) {
91-
const user = await response.json();
92-
if (user) {
93-
return user;
94-
} else {
95-
return null;
96-
}
97-
} else if (response.status === 401) {
98-
await TokenStorage.removeToken();
99-
return null;
108+
await this.handleResponse(response);
109+
110+
const user = await response.json();
111+
112+
if (user) {
113+
return user;
100114
} else {
101-
throw new Error('The request was not successful');
115+
return null;
102116
}
103117
} catch (error) {
104118
console.error('Fehler beim Abrufen des Benutzers:', error);

0 commit comments

Comments
 (0)