1 | import { Injectable } from '@angular/core';
|
---|
2 | import { User, UserManager } from 'oidc-client';
|
---|
3 | import { Observable, of } from 'rxjs';
|
---|
4 |
|
---|
5 | import { environment } from '@env/environment';
|
---|
6 | import { ApplicationUser, ApplicationUserType, SelfUserResponse } from 'src/app/shared-app/models';
|
---|
7 | import { BaseApiService } from 'src/app/shared-app/services/base-api.service';
|
---|
8 |
|
---|
9 | @Injectable({
|
---|
10 | providedIn: 'root'
|
---|
11 | })
|
---|
12 | export class AuthService {
|
---|
13 | private userManager: UserManager = new UserManager({
|
---|
14 | authority: environment.identityRoute,
|
---|
15 | client_id: environment.identityClientId,
|
---|
16 | redirect_uri: `${window.location.origin}/auth-callback`,
|
---|
17 | response_type: 'id_token token',
|
---|
18 | scope: 'openid app.api.finki-chattery profile',
|
---|
19 | post_logout_redirect_uri: window.location.origin,
|
---|
20 | filterProtocolClaims: true,
|
---|
21 | loadUserInfo: true
|
---|
22 | });
|
---|
23 |
|
---|
24 | public user: ApplicationUser | null = null;
|
---|
25 | public oidcUser: User | null = null;
|
---|
26 | public selfUser: SelfUserResponse | null = null;
|
---|
27 |
|
---|
28 | constructor(private baseApi: BaseApiService) {
|
---|
29 | this.userManager.getUser().then((user) => {
|
---|
30 | this.oidcUser = user;
|
---|
31 | this.user = new ApplicationUser(
|
---|
32 | user?.profile.id,
|
---|
33 | user?.profile.userType,
|
---|
34 | user?.profile.emailAddress,
|
---|
35 | user?.profile.username,
|
---|
36 | user?.profile.isVerified
|
---|
37 | );
|
---|
38 |
|
---|
39 | this.selfUserDto().subscribe((selfUser) => {
|
---|
40 | if (selfUser) {
|
---|
41 | this.selfUser = selfUser;
|
---|
42 | }
|
---|
43 | });
|
---|
44 | });
|
---|
45 | }
|
---|
46 |
|
---|
47 | public login(): void {
|
---|
48 | this.userManager.signinRedirect();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public logout(): void {
|
---|
52 | this.userManager.signoutRedirect();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public isLoggedIn(): boolean {
|
---|
56 | if (this.oidcUser) {
|
---|
57 | return !this.oidcUser.expired;
|
---|
58 | }
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public isStudent(): boolean {
|
---|
63 | return this.user !== null && this.user.userType === ApplicationUserType.Student;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public isModerator(): boolean {
|
---|
67 | return this.user !== null && this.user.userType === ApplicationUserType.Moderator;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public currentUser(): ApplicationUser | null {
|
---|
71 | return this.user;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public currentUserToken(): string {
|
---|
75 | if (this.oidcUser) {
|
---|
76 | return this.oidcUser.access_token;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return '';
|
---|
80 | }
|
---|
81 |
|
---|
82 | public selfUserDto(): Observable<SelfUserResponse | null> {
|
---|
83 | if (this.isLoggedIn()) {
|
---|
84 | return this.baseApi.getSelfUser();
|
---|
85 | }
|
---|
86 | return of(null);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public async completeAuthentication(): Promise<void> {
|
---|
90 | return await this.userManager.signinRedirectCallback().then((user: User) => {
|
---|
91 | this.oidcUser = user;
|
---|
92 | this.user = new ApplicationUser(
|
---|
93 | user.profile.id,
|
---|
94 | user.profile.userType,
|
---|
95 | user.profile.emailAddress,
|
---|
96 | user.profile.username,
|
---|
97 | user.profile.isVerified
|
---|
98 | );
|
---|
99 |
|
---|
100 | if (!this.selfUser) {
|
---|
101 | this.selfUserDto().subscribe((selfUser) => {
|
---|
102 | if (selfUser) {
|
---|
103 | this.selfUser = selfUser;
|
---|
104 | }
|
---|
105 | });
|
---|
106 | }
|
---|
107 | });
|
---|
108 | }
|
---|
109 | }
|
---|