diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts new file mode 100644 index 0000000000..f7dd03267e --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts @@ -0,0 +1,26 @@ +import { + HttpEvent, + HttpHandler, + HttpHeaders, + HttpInterceptor, + HttpRequest, +} from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root', +}) +export class ApiInterceptor implements IApiInterceptor { + getAdditionalHeaders(existingHeaders?: HttpHeaders) { + return existingHeaders; + } + + intercept(req: HttpRequest, next: HttpHandler): Observable> { + return next.handle(req); + } +} + +export interface IApiInterceptor extends HttpInterceptor { + getAdditionalHeaders(existingHeaders?: HttpHeaders): HttpHeaders; +} diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/index.ts b/npm/ng-packs/packages/core/src/lib/interceptors/index.ts new file mode 100644 index 0000000000..d7479a7684 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/interceptors/index.ts @@ -0,0 +1 @@ +export * from './api.interceptor'; diff --git a/npm/ng-packs/packages/core/src/lib/models/auth.ts b/npm/ng-packs/packages/core/src/lib/models/auth.ts index 5621dc8655..4a1a01f357 100644 --- a/npm/ng-packs/packages/core/src/lib/models/auth.ts +++ b/npm/ng-packs/packages/core/src/lib/models/auth.ts @@ -1,6 +1,16 @@ +import { UnaryFunction } from 'rxjs'; +import { Injector } from '@angular/core'; + export interface LoginParams { username: string; password: string; rememberMe?: boolean; redirectUrl?: string; } + +export type PipeToLoginFn = ( + params: Pick, + injector: Injector, +) => UnaryFunction; + +export type SetTokenResponseToStorageFn = (injector: Injector, tokenRes: unknown) => void; diff --git a/npm/ng-packs/packages/core/src/lib/tokens/index.ts b/npm/ng-packs/packages/core/src/lib/tokens/index.ts index 20cab35c28..45b0f9f3cd 100644 --- a/npm/ng-packs/packages/core/src/lib/tokens/index.ts +++ b/npm/ng-packs/packages/core/src/lib/tokens/index.ts @@ -6,4 +6,6 @@ export * from './lodaer-delay.token'; export * from './manage-profile.token'; export * from './options.token'; export * from './tenant-key.token'; -export * from './include-localization-resources.token'; \ No newline at end of file +export * from './include-localization-resources.token'; +export * from './pipe-to-login.token'; +export * from './set-token-response-to-storage.token'; diff --git a/npm/ng-packs/packages/core/src/lib/tokens/pipe-to-login.token.ts b/npm/ng-packs/packages/core/src/lib/tokens/pipe-to-login.token.ts new file mode 100644 index 0000000000..3100ede9bb --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tokens/pipe-to-login.token.ts @@ -0,0 +1,4 @@ +import { InjectionToken } from '@angular/core'; +import { PipeToLoginFn } from '../models/auth'; + +export const PIPE_TO_LOGIN_FN_KEY = new InjectionToken('PIPE_TO_LOGIN_FN_KEY'); diff --git a/npm/ng-packs/packages/core/src/lib/tokens/set-token-response-to-storage.token.ts b/npm/ng-packs/packages/core/src/lib/tokens/set-token-response-to-storage.token.ts new file mode 100644 index 0000000000..0436195654 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tokens/set-token-response-to-storage.token.ts @@ -0,0 +1,6 @@ +import { InjectionToken } from '@angular/core'; +import { SetTokenResponseToStorageFn } from '../models'; + +export const SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY = new InjectionToken( + 'SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY', +); diff --git a/npm/ng-packs/packages/core/src/public-api.ts b/npm/ng-packs/packages/core/src/public-api.ts index 20dec1308b..da5fccd8c0 100644 --- a/npm/ng-packs/packages/core/src/public-api.ts +++ b/npm/ng-packs/packages/core/src/public-api.ts @@ -22,3 +22,4 @@ export * from './lib/strategies'; export * from './lib/tokens'; export * from './lib/utils'; export * from './lib/validators'; +export * from './lib/interceptors'; diff --git a/npm/ng-packs/packages/oauth/src/lib/interceptors/api.interceptor.ts b/npm/ng-packs/packages/oauth/src/lib/interceptors/api.interceptor.ts index 91666b12a9..e807133c0d 100644 --- a/npm/ng-packs/packages/oauth/src/lib/interceptors/api.interceptor.ts +++ b/npm/ng-packs/packages/oauth/src/lib/interceptors/api.interceptor.ts @@ -1,13 +1,13 @@ -import { HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from '@angular/common/http'; +import { HttpHandler, HttpHeaders, HttpRequest } from '@angular/common/http'; import { Inject, Injectable } from '@angular/core'; import { OAuthService } from 'angular-oauth2-oidc'; import { finalize } from 'rxjs/operators'; -import { SessionStateService, HttpWaitService, TENANT_KEY } from '@abp/ng.core'; +import { SessionStateService, HttpWaitService, TENANT_KEY, IApiInterceptor } from '@abp/ng.core'; @Injectable({ providedIn: 'root', }) -export class ApiInterceptor implements HttpInterceptor { +export class OAuthApiInterceptor implements IApiInterceptor { constructor( private oAuthService: OAuthService, private sessionState: SessionStateService, diff --git a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts index 0bc0c887ca..ca9f232015 100644 --- a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts +++ b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts @@ -1,15 +1,23 @@ import { APP_INITIALIZER, Injector, ModuleWithProviders, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { OAuthModule, OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; -import { AuthGuard, AuthService, noop } from '@abp/ng.core'; +import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc'; +import { + ApiInterceptor, + AuthGuard, + AuthService, + noop, + PIPE_TO_LOGIN_FN_KEY, + SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY, +} from '@abp/ng.core'; import { storageFactory } from './utils/storage.factory'; import { AbpOAuthService, TimeoutLimitedOAuthService } from './services'; import { OAuthConfigurationHandler } from './handlers/oauth-configuration.handler'; import { initFactory } from './utils/init-factory'; import { HTTP_INTERCEPTORS } from '@angular/common/http'; -import { ApiInterceptor } from './interceptors/api.interceptor'; +import { OAuthApiInterceptor } from './interceptors/api.interceptor'; import { AbpOAuthGuard } from './guards/oauth.guard'; import { NavigateToManageProfileProvider } from './providers'; +import { pipeToLogin, setTokenResponseToStorage } from './utils'; @NgModule({ imports: [CommonModule, OAuthModule], @@ -27,6 +35,18 @@ export class AbpOAuthModule { provide: AuthGuard, useClass: AbpOAuthGuard, }, + { + provide: ApiInterceptor, + useClass: OAuthApiInterceptor, + }, + { + provide: PIPE_TO_LOGIN_FN_KEY, + useValue: pipeToLogin, + }, + { + provide: SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY, + useValue: setTokenResponseToStorage, + }, { provide: HTTP_INTERCEPTORS, useExisting: ApiInterceptor, @@ -45,7 +65,6 @@ export class AbpOAuthModule { deps: [Injector], useFactory: initFactory, }, - OAuthModule.forRoot().providers, { provide: OAuthStorage, useFactory: storageFactory }, // {provide: OAuthService, useClass: TimeoutLimitedOAuthService} diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 2ac24abf22..f620aadf77 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -3,12 +3,17 @@ import { Router } from '@angular/router'; import { OAuthStorage, TokenResponse } from 'angular-oauth2-oidc'; import { pipe } from 'rxjs'; import { switchMap, tap } from 'rxjs/operators'; -import { ConfigStateService, LoginParams } from '@abp/ng.core'; +import { + ConfigStateService, + LoginParams, + PipeToLoginFn, + SetTokenResponseToStorageFn, +} from '@abp/ng.core'; const cookieKey = 'rememberMe'; const storageKey = 'passwordFlow'; -export function pipeToLogin( +export const pipeToLogin: PipeToLoginFn = function ( params: Pick, injector: Injector, ) { @@ -22,10 +27,18 @@ export function pipeToLogin( if (params.redirectUrl) router.navigate([params.redirectUrl]); }), ); -} +}; -export function setTokenResponseToStorage(injector: Injector, tokenRes: TokenResponse) { - const { access_token, refresh_token, scope: grantedScopes, expires_in } = tokenRes; +export const setTokenResponseToStorage: SetTokenResponseToStorageFn = function ( + injector: Injector, + tokenRes: unknown, +) { + const { + access_token, + refresh_token, + scope: grantedScopes, + expires_in, + } = tokenRes as TokenResponse; const storage = injector.get(OAuthStorage); storage.setItem('access_token', access_token); @@ -42,7 +55,7 @@ export function setTokenResponseToStorage(injector: Injector, tokenRes: TokenRes const expiresAt = now.getTime() + expiresInMilliSeconds; storage.setItem('expires_at', '' + expiresAt); } -} +}; export function setRememberMe(remember: boolean) { removeRememberMe();