-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.service.ts
More file actions
114 lines (94 loc) · 2.68 KB
/
Copy pathauth.service.ts
File metadata and controls
114 lines (94 loc) · 2.68 KB
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
105
106
107
108
109
110
111
112
113
114
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { computed, inject, Injectable, signal } from '@angular/core';
import { AuthKeys } from '@gh/shared/models';
import { loggedMethod } from '@gh/shared/utils';
import { StoreService } from 'core/services/store/store.service';
import { publicGet, publicPost, refreshPost } from 'core/utils/api';
import { CookieService } from 'ngx-cookie-service';
import { catchError, of, tap } from 'rxjs';
type Credentials = {
accessToken: string;
refreshToken: string;
};
@Injectable({
providedIn: 'root',
})
export class AuthService {
readonly #storeService = inject(StoreService);
readonly #http = inject(HttpClient);
readonly #cookieService = inject(CookieService);
readonly #baseApiUrl = '/api';
#error = signal<HttpErrorResponse | undefined>(undefined);
serverError = computed(() => {
const error = this.#error();
return error?.status && error.status >= 500;
});
get authenticated() {
return this.#storeService.authenticated;
}
get credentials() {
return {
accessToken: this.#cookieService.get(AuthKeys.AccessToken),
refreshToken: this.#cookieService.get(AuthKeys.RefreshToken),
} as Credentials;
}
clearCredentials() {
this.#storeService.authenticated = false;
this.#cookieService.delete(AuthKeys.AccessToken);
this.#cookieService.delete(AuthKeys.RefreshToken);
}
saveCredentials() {
this.#storeService.authenticated = true;
}
get accessToken() {
return this.credentials.accessToken;
}
get refreshToken() {
return this.credentials.refreshToken;
}
get lastError() {
return this.#error()?.status;
}
@loggedMethod()
isConnected() {
return publicGet<boolean>(this.#http, `${this.#baseApiUrl}/auth/connected`);
}
@loggedMethod()
login(email: string, password: string) {
const url = `${this.#baseApiUrl}/auth/login`;
const credentials = { email, password };
this.#error.set(undefined);
return publicPost(this.#http, url, credentials).pipe(
catchError((error) => {
this.#error.set(error);
if (error instanceof HttpErrorResponse) {
console.error('http error:', error);
} else {
console.error('error:', error);
}
this.clearCredentials();
return of(null);
}),
tap(() => {
if (this.accessToken && this.refreshToken) {
this.saveCredentials();
}
}),
);
}
@loggedMethod()
loginGoogle() {
const url = `${this.#baseApiUrl}/auth/google/login`;
window.location.href = url;
}
@loggedMethod()
logout() {
this.clearCredentials();
this.#storeService.resetUserCards();
}
@loggedMethod()
refresh(refreshToken: string) {
const url = `${this.#baseApiUrl}/auth/refresh`;
return refreshPost(this.#http, url, { refreshToken });
}
}