Skip to content

Commit

Permalink
Merge pull request abpframework#9372 from abpframework/feat/auth-utils
Browse files Browse the repository at this point in the history
Angular UI: Added an utility function called pipeToTokenResponse
  • Loading branch information
mehmet-erim authored Jun 21, 2021
2 parents 667e981 + 675ed28 commit 0033873
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CurrentTenantDto, MultiTenancyInfoDto } from '../multi-tenancy/models';
import type { ObjectExtensionsDto } from './object-extending/models';
import type { LanguageInfo } from '../../../localization/models';
import type { NameValue } from '../../../models';
import type { CurrentTenantDto, MultiTenancyInfoDto } from '../multi-tenancy/models';
import type { ObjectExtensionsDto } from './object-extending/models';

export interface ApplicationAuthConfigurationDto {
policies: Record<string, boolean>;
Expand Down Expand Up @@ -66,6 +66,8 @@ export interface CurrentUserDto {
phoneNumber?: string;
phoneNumberVerified: boolean;
roles: string[];
impersonatorUserId?: string;
impersonatorTenantId?: string;
}

export interface DateTimeFormatDto {
Expand Down
21 changes: 21 additions & 0 deletions npm/ng-packs/packages/core/src/lib/utils/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injector } from '@angular/core';
import { Router } from '@angular/router';
import { OAuthStorage, TokenResponse } from 'angular-oauth2-oidc';
import { pipe } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { LoginParams } from '../models/auth';
Expand Down Expand Up @@ -27,6 +28,26 @@ export function pipeToLogin(
);
}

export function setTokenResponseToStorage(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');
Expand Down

0 comments on commit 0033873

Please sign in to comment.