|
|
|
@ -1,15 +1,16 @@ |
|
|
|
import { Injectable, inject } from '@angular/core'; |
|
|
|
import { Injectable, inject, PLATFORM_ID, RESPONSE_INIT } from '@angular/core'; |
|
|
|
import { |
|
|
|
UrlTree, |
|
|
|
ActivatedRouteSnapshot, |
|
|
|
RouterStateSnapshot, |
|
|
|
CanActivateFn, |
|
|
|
CanActivateFn, Params, |
|
|
|
} from '@angular/router'; |
|
|
|
|
|
|
|
import { Observable, timer, filter, take, map, firstValueFrom, timeout, catchError, of } from 'rxjs'; |
|
|
|
import { OAuthService } from 'angular-oauth2-oidc'; |
|
|
|
|
|
|
|
import { AuthService, IAbpGuard, EnvironmentService } from '@abp/ng.core'; |
|
|
|
import { isPlatformServer } from '@angular/common'; |
|
|
|
|
|
|
|
/** |
|
|
|
* @deprecated Use `abpOAuthGuard` *function* instead. |
|
|
|
@ -42,6 +43,9 @@ export const abpOAuthGuard: CanActivateFn = ( |
|
|
|
) => { |
|
|
|
const oAuthService = inject(OAuthService); |
|
|
|
const authService = inject(AuthService); |
|
|
|
const platformId = inject(PLATFORM_ID); |
|
|
|
const resInit = inject(RESPONSE_INIT); |
|
|
|
const environmentService = inject(EnvironmentService); |
|
|
|
|
|
|
|
const hasValidAccessToken = oAuthService.hasValidAccessToken(); |
|
|
|
|
|
|
|
@ -50,10 +54,30 @@ export const abpOAuthGuard: CanActivateFn = ( |
|
|
|
} |
|
|
|
|
|
|
|
const params = { returnUrl: state.url }; |
|
|
|
if (isPlatformServer(platformId) && resInit) { |
|
|
|
const ssrAuthorizationUrl = environmentService.getEnvironment().oAuthConfig.ssrAuthorizationUrl; |
|
|
|
const url = buildLoginUrl(ssrAuthorizationUrl, params); |
|
|
|
const headers = new Headers(resInit.headers); |
|
|
|
headers.set('Location', url); |
|
|
|
resInit.status = 302; |
|
|
|
resInit.statusText = 'Found'; |
|
|
|
resInit.headers = headers; |
|
|
|
return; |
|
|
|
} |
|
|
|
authService.navigateToLogin(params); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
export const buildLoginUrl = (path: string, params?: Params): string => { |
|
|
|
if (!params || Object.keys(params).length === 0) return path; |
|
|
|
const usp = new URLSearchParams(); |
|
|
|
for (const [k, v] of Object.entries(params)) { |
|
|
|
if (v == null) continue; |
|
|
|
Array.isArray(v) ? v.forEach(x => usp.append(k, String(x))) : usp.set(k, String(v)); |
|
|
|
} |
|
|
|
return `${path}?${usp.toString()}`; |
|
|
|
} |
|
|
|
|
|
|
|
export const asyncAbpOAuthGuard: CanActivateFn = ( |
|
|
|
route: ActivatedRouteSnapshot, |
|
|
|
state: RouterStateSnapshot, |
|
|
|
|