From 26b9fbc119c34f8087e5ad043166a001eabc62f6 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Wed, 15 Feb 2023 18:44:24 +0300 Subject: [PATCH] remove unnescessary abstraction --- .../packages/core/src/lib/models/auth.ts | 6 +++-- .../set-token-response-to-storage.token.ts | 3 +++ .../packages/oauth/src/lib/oauth.module.ts | 11 +++----- .../oauth/src/lib/utils/auth-utils.ts | 25 +------------------ 4 files changed, 11 insertions(+), 34 deletions(-) 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 9c0250d316..dcaf6e7f11 100644 --- a/npm/ng-packs/packages/core/src/lib/models/auth.ts +++ b/npm/ng-packs/packages/core/src/lib/models/auth.ts @@ -12,6 +12,8 @@ export type PipeToLoginFn = ( params: Pick, injector: Injector, ) => UnaryFunction; - -export type SetTokenResponseToStorageFn = (injector: Injector, tokenRes: T) => void; +/** + * @deprecated The interface should not be used anymore. + */ +export type SetTokenResponseToStorageFn = (tokenRes: T) => void; export type CheckAuthenticationStateFn = (injector: Injector) => void; 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 index 0436195654..0cd26708bf 100644 --- 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 @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; import { SetTokenResponseToStorageFn } from '../models'; +/** + * @deprecated The token should not be used anymore. + */ 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/oauth/src/lib/oauth.module.ts b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts index 753ad815e3..7c8c2af99c 100644 --- a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts +++ b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts @@ -1,4 +1,4 @@ -import { APP_INITIALIZER, Injector, ModuleWithProviders, NgModule } from '@angular/core'; +import { APP_INITIALIZER, ModuleWithProviders, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc'; import { @@ -7,8 +7,7 @@ import { AuthService, CHECK_AUTHENTICATION_STATE_FN_KEY, noop, - PIPE_TO_LOGIN_FN_KEY, - SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY, + PIPE_TO_LOGIN_FN_KEY } from '@abp/ng.core'; import { storageFactory } from './utils/storage.factory'; import { AbpOAuthService } from './services'; @@ -17,7 +16,7 @@ import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { OAuthApiInterceptor } from './interceptors/api.interceptor'; import { AbpOAuthGuard } from './guards/oauth.guard'; import { NavigateToManageProfileProvider } from './providers'; -import { checkAccessToken, pipeToLogin, setTokenResponseToStorage } from './utils'; +import { checkAccessToken, pipeToLogin } from './utils'; @NgModule({ imports: [CommonModule, OAuthModule], @@ -43,10 +42,6 @@ export class AbpOAuthModule { provide: PIPE_TO_LOGIN_FN_KEY, useValue: pipeToLogin, }, - { - provide: SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY, - useValue: setTokenResponseToStorage, - }, { provide: CHECK_AUTHENTICATION_STATE_FN_KEY, useValue: checkAccessToken, 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 74df5ce79b..64dcd76576 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 @@ -1,4 +1,4 @@ -import { Injector } from '@angular/core'; +import { inject, Injector } from '@angular/core'; import { Router } from '@angular/router'; import { OAuthStorage, TokenResponse } from 'angular-oauth2-oidc'; import { pipe } from 'rxjs'; @@ -29,29 +29,6 @@ export const pipeToLogin: PipeToLoginFn = function ( ); }; -export const setTokenResponseToStorage: SetTokenResponseToStorageFn = function ( - injector: Injector, - tokenRes: TokenResponse, -) { - const { access_token, refresh_token, scope: grantedScopes, expires_in } = tokenRes; - const storage = injector.get(OAuthStorage); - - storage.setItem('access_token', access_token); - storage.setItem('refresh_token', refresh_token); - storage.setItem('access_token_stored_at', '' + Date.now()); - - if (grantedScopes) { - storage.setItem('granted_scopes', JSON.stringify(grantedScopes.split(' '))); - } - - if (expires_in) { - const expiresInMilliSeconds = expires_in * 1000; - const now = new Date(); - const expiresAt = now.getTime() + expiresInMilliSeconds; - storage.setItem('expires_at', '' + expiresAt); - } -}; - export function setRememberMe(remember: boolean) { removeRememberMe(); localStorage.setItem(storageKey, 'true');