|
1 | | -import { Injectable, inject } from '@angular/core'; |
2 | | -import { |
3 | | - ActivatedRouteSnapshot, |
4 | | - Router, |
5 | | - RouterStateSnapshot, |
6 | | -} from '@angular/router'; |
7 | | -import { Observable } from 'rxjs'; |
8 | | -import { map } from 'rxjs/operators'; |
9 | | -import { AuthOptions } from '../auth-options'; |
10 | | -import { AuthStateService } from '../auth-state/auth-state.service'; |
11 | | -import { ConfigurationService } from '../config/config.service'; |
12 | | -import { LoginService } from '../login/login.service'; |
13 | | -import { AutoLoginService } from './auto-login.service'; |
14 | | - |
15 | | -@Injectable({ providedIn: 'root' }) |
16 | | -export class AutoLoginPartialRoutesGuard { |
17 | | - private readonly autoLoginService = inject(AutoLoginService); |
18 | | - |
19 | | - private readonly authStateService = inject(AuthStateService); |
20 | | - |
21 | | - private readonly loginService = inject(LoginService); |
22 | | - |
23 | | - private readonly configurationService = inject(ConfigurationService); |
24 | | - |
25 | | - private readonly router = inject(Router); |
26 | | - |
27 | | - canLoad(): Observable<boolean> { |
28 | | - const url = |
29 | | - this.router |
30 | | - .getCurrentNavigation() |
31 | | - ?.extractedUrl.toString() |
32 | | - .substring(1) ?? ''; |
33 | | - |
34 | | - return checkAuth( |
35 | | - url, |
36 | | - this.configurationService, |
37 | | - this.authStateService, |
38 | | - this.autoLoginService, |
39 | | - this.loginService |
40 | | - ); |
41 | | - } |
42 | | - |
43 | | - canActivate( |
44 | | - route: ActivatedRouteSnapshot, |
45 | | - state: RouterStateSnapshot |
46 | | - ): Observable<boolean> { |
47 | | - const authOptions: AuthOptions | undefined = route?.data |
48 | | - ? { customParams: route.data } |
49 | | - : undefined; |
50 | | - |
51 | | - return checkAuth( |
52 | | - state.url, |
53 | | - this.configurationService, |
54 | | - this.authStateService, |
55 | | - this.autoLoginService, |
56 | | - this.loginService, |
57 | | - authOptions |
58 | | - ); |
59 | | - } |
60 | | - |
61 | | - canActivateChild( |
62 | | - route: ActivatedRouteSnapshot, |
63 | | - state: RouterStateSnapshot |
64 | | - ): Observable<boolean> { |
65 | | - const authOptions: AuthOptions | undefined = route?.data |
66 | | - ? { customParams: route.data } |
67 | | - : undefined; |
68 | | - |
69 | | - return checkAuth( |
70 | | - state.url, |
71 | | - this.configurationService, |
72 | | - this.authStateService, |
73 | | - this.autoLoginService, |
74 | | - this.loginService, |
75 | | - authOptions |
76 | | - ); |
77 | | - } |
78 | | -} |
79 | | - |
80 | | -export function autoLoginPartialRoutesGuard( |
81 | | - route?: ActivatedRouteSnapshot, |
82 | | - state?: RouterStateSnapshot, |
83 | | - configId?: string |
84 | | -): Observable<boolean> { |
85 | | - const configurationService = inject(ConfigurationService); |
86 | | - const authStateService = inject(AuthStateService); |
87 | | - const loginService = inject(LoginService); |
88 | | - const autoLoginService = inject(AutoLoginService); |
89 | | - const router = inject(Router); |
90 | | - const authOptions: AuthOptions | undefined = route?.data |
91 | | - ? { customParams: route.data } |
92 | | - : undefined; const url = |
93 | | - router.getCurrentNavigation()?.extractedUrl.toString().substring(1) ?? ''; |
94 | | - |
95 | | - return checkAuth( |
96 | | - url, |
97 | | - configurationService, |
98 | | - authStateService, |
99 | | - autoLoginService, |
100 | | - loginService, |
101 | | - authOptions, |
102 | | - configId |
103 | | - ); |
104 | | -} |
105 | | - |
106 | | -export function autoLoginPartialRoutesGuardWithConfig( |
107 | | - configId: string |
108 | | -): ( |
109 | | - route?: ActivatedRouteSnapshot, |
110 | | - state?: RouterStateSnapshot |
111 | | -) => Observable<boolean> { |
112 | | - return (route?: ActivatedRouteSnapshot, state?: RouterStateSnapshot) => |
113 | | - autoLoginPartialRoutesGuard(route, state, configId); |
114 | | -} |
115 | | - |
116 | | -function checkAuth( |
117 | | - url: string, |
118 | | - configurationService: ConfigurationService, |
119 | | - authStateService: AuthStateService, |
120 | | - autoLoginService: AutoLoginService, |
121 | | - loginService: LoginService, |
122 | | - authOptions?: AuthOptions, |
123 | | - configId?: string |
124 | | -): Observable<boolean> { |
125 | | - return configurationService.getOpenIDConfiguration(configId).pipe( |
126 | | - map((configuration) => { |
127 | | - const isAuthenticated = |
128 | | - authStateService.areAuthStorageTokensValid(configuration); |
129 | | - |
130 | | - if (isAuthenticated) { |
131 | | - autoLoginService.checkSavedRedirectRouteAndNavigate(configuration); |
132 | | - } |
133 | | - |
134 | | - if (!isAuthenticated) { |
135 | | - autoLoginService.saveRedirectRoute(configuration, url); |
136 | | - if (authOptions) { |
137 | | - loginService.login(configuration, authOptions); |
138 | | - } else { |
139 | | - loginService.login(configuration); |
140 | | - } |
141 | | - } |
142 | | - |
143 | | - return isAuthenticated; |
144 | | - }) |
145 | | - ); |
146 | | -} |
| 1 | +import { inject, Injectable } from '@angular/core'; |
| 2 | +import { |
| 3 | + ActivatedRouteSnapshot, |
| 4 | + Router, |
| 5 | + RouterStateSnapshot, |
| 6 | +} from '@angular/router'; |
| 7 | +import { Observable } from 'rxjs'; |
| 8 | +import { map } from 'rxjs/operators'; |
| 9 | +import { AuthOptions } from '../auth-options'; |
| 10 | +import { AuthStateService } from '../auth-state/auth-state.service'; |
| 11 | +import { ConfigurationService } from '../config/config.service'; |
| 12 | +import { LoginService } from '../login/login.service'; |
| 13 | +import { AutoLoginService } from './auto-login.service'; |
| 14 | + |
| 15 | +@Injectable({ providedIn: 'root' }) |
| 16 | +export class AutoLoginPartialRoutesGuard { |
| 17 | + private readonly autoLoginService = inject(AutoLoginService); |
| 18 | + private readonly authStateService = inject(AuthStateService); |
| 19 | + private readonly loginService = inject(LoginService); |
| 20 | + private readonly configurationService = inject(ConfigurationService); |
| 21 | + private readonly router = inject(Router); |
| 22 | + |
| 23 | + canLoad(): Observable<boolean> { |
| 24 | + const url = |
| 25 | + this.router |
| 26 | + .getCurrentNavigation() |
| 27 | + ?.extractedUrl.toString() |
| 28 | + .substring(1) ?? ''; |
| 29 | + |
| 30 | + return checkAuth( |
| 31 | + url, |
| 32 | + this.configurationService, |
| 33 | + this.authStateService, |
| 34 | + this.autoLoginService, |
| 35 | + this.loginService |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + canActivate( |
| 40 | + route: ActivatedRouteSnapshot, |
| 41 | + state: RouterStateSnapshot |
| 42 | + ): Observable<boolean> { |
| 43 | + const authOptions: AuthOptions | undefined = route?.data |
| 44 | + ? { customParams: route.data } |
| 45 | + : undefined; |
| 46 | + |
| 47 | + return checkAuth( |
| 48 | + state.url, |
| 49 | + this.configurationService, |
| 50 | + this.authStateService, |
| 51 | + this.autoLoginService, |
| 52 | + this.loginService, |
| 53 | + authOptions |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + canActivateChild( |
| 58 | + route: ActivatedRouteSnapshot, |
| 59 | + state: RouterStateSnapshot |
| 60 | + ): Observable<boolean> { |
| 61 | + const authOptions: AuthOptions | undefined = route?.data |
| 62 | + ? { customParams: route.data } |
| 63 | + : undefined; |
| 64 | + |
| 65 | + return checkAuth( |
| 66 | + state.url, |
| 67 | + this.configurationService, |
| 68 | + this.authStateService, |
| 69 | + this.autoLoginService, |
| 70 | + this.loginService, |
| 71 | + authOptions |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function autoLoginPartialRoutesGuard( |
| 77 | + route?: ActivatedRouteSnapshot, |
| 78 | + state?: RouterStateSnapshot, |
| 79 | + configId?: string |
| 80 | +): Observable<boolean> { |
| 81 | + const configurationService = inject(ConfigurationService); |
| 82 | + const authStateService = inject(AuthStateService); |
| 83 | + const loginService = inject(LoginService); |
| 84 | + const autoLoginService = inject(AutoLoginService); |
| 85 | + const router = inject(Router); |
| 86 | + const authOptions: AuthOptions | undefined = route?.data |
| 87 | + ? { customParams: route.data } |
| 88 | + : undefined; |
| 89 | + const url = |
| 90 | + router.getCurrentNavigation()?.extractedUrl.toString().substring(1) ?? ''; |
| 91 | + |
| 92 | + return checkAuth( |
| 93 | + url, |
| 94 | + configurationService, |
| 95 | + authStateService, |
| 96 | + autoLoginService, |
| 97 | + loginService, |
| 98 | + authOptions, |
| 99 | + configId |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +export function autoLoginPartialRoutesGuardWithConfig( |
| 104 | + configId: string |
| 105 | +): ( |
| 106 | + route?: ActivatedRouteSnapshot, |
| 107 | + state?: RouterStateSnapshot |
| 108 | +) => Observable<boolean> { |
| 109 | + return (route?: ActivatedRouteSnapshot, state?: RouterStateSnapshot) => |
| 110 | + autoLoginPartialRoutesGuard(route, state, configId); |
| 111 | +} |
| 112 | + |
| 113 | +function checkAuth( |
| 114 | + url: string, |
| 115 | + configurationService: ConfigurationService, |
| 116 | + authStateService: AuthStateService, |
| 117 | + autoLoginService: AutoLoginService, |
| 118 | + loginService: LoginService, |
| 119 | + authOptions?: AuthOptions, |
| 120 | + configId?: string |
| 121 | +): Observable<boolean> { |
| 122 | + return configurationService.getOpenIDConfiguration(configId).pipe( |
| 123 | + map((configuration) => { |
| 124 | + const isAuthenticated = |
| 125 | + authStateService.areAuthStorageTokensValid(configuration); |
| 126 | + |
| 127 | + if (isAuthenticated) { |
| 128 | + autoLoginService.checkSavedRedirectRouteAndNavigate(configuration); |
| 129 | + } |
| 130 | + |
| 131 | + if (!isAuthenticated) { |
| 132 | + autoLoginService.saveRedirectRoute(configuration, url); |
| 133 | + if (authOptions) { |
| 134 | + loginService.login(configuration, authOptions); |
| 135 | + } else { |
| 136 | + loginService.login(configuration); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return isAuthenticated; |
| 141 | + }) |
| 142 | + ); |
| 143 | +} |
0 commit comments