forked from CenterForOpenScience/angular-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.service.ts
More file actions
114 lines (91 loc) · 3.17 KB
/
auth.service.ts
File metadata and controls
114 lines (91 loc) · 3.17 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 { createDispatchMap } from '@ngxs/store';
import { CookieService } from 'ngx-cookie-service';
import { isPlatformBrowser } from '@angular/common';
import { inject, Injectable, PLATFORM_ID } from '@angular/core';
import { SignUpModel } from '@core/models/sign-up.model';
import { ENVIRONMENT } from '@core/provider/environment.provider';
import { ClearCurrentUser } from '@osf/core/store/user';
import { urlParam } from '@osf/shared/helpers/url-param.helper';
import { JsonApiService } from '@osf/shared/services/json-api.service';
import { LoaderService } from '@osf/shared/services/loader.service';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private readonly jsonApiService = inject(JsonApiService);
private readonly cookieService = inject(CookieService);
private readonly loaderService = inject(LoaderService);
private readonly environment = inject(ENVIRONMENT);
private readonly platformId = inject(PLATFORM_ID);
private readonly actions = createDispatchMap({ clearCurrentUser: ClearCurrentUser });
get apiUrl() {
return `${this.environment.apiDomainUrl}/v2/users/`;
}
get webUrl() {
return this.environment.webUrl;
}
get casUrl() {
return this.environment.casUrl;
}
navigateToSignIn(): void {
if (!isPlatformBrowser(this.platformId)) {
return;
}
this.loaderService.show();
const loginUrl = `${this.casUrl}/login?${urlParam({ service: `${this.webUrl}/login`, next: window.location.href })}`;
window.location.href = loginUrl;
}
navigateToOrcidSignIn(): void {
if (!isPlatformBrowser(this.platformId)) {
return;
}
const loginUrl = `${this.casUrl}/login?${urlParam({
redirectOrcid: 'true',
service: `${this.webUrl}/login`,
next: window.location.href,
})}`;
window.location.href = loginUrl;
}
navigateToInstitutionSignIn(): void {
if (!isPlatformBrowser(this.platformId)) {
return;
}
const loginUrl = `${this.casUrl}/login?${urlParam({
campaign: 'institution',
service: `${this.webUrl}/login`,
next: window.location.href,
})}`;
window.location.href = loginUrl;
}
logout(nextUrl?: string): void {
this.loaderService.show();
this.actions.clearCurrentUser();
if (isPlatformBrowser(this.platformId)) {
this.cookieService.deleteAll();
window.location.href = `${this.webUrl}/logout/?next=${encodeURIComponent(nextUrl || '/')}`;
}
}
register(payload: SignUpModel) {
const baseUrl = `${this.webUrl}/api/v1/register/`;
const body = { ...payload, 'g-recaptcha-response': payload.recaptcha, campaign: null };
return this.jsonApiService.post(baseUrl, body);
}
forgotPassword(email: string) {
const baseUrl = `${this.apiUrl}/reset_password/`;
const params: Record<string, string> = { email };
return this.jsonApiService.get(baseUrl, params);
}
resetPassword(userId: string, token: string, newPassword: string) {
const baseUrl = `${this.apiUrl}/reset_password/`;
const body = {
data: {
attributes: {
uid: userId,
token,
password: newPassword,
},
},
};
return this.jsonApiService.post(baseUrl, body);
}
}