|
|
|
@ -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';
|
|
|
|
@ -27,6 +28,31 @@ export function pipeToLogin(
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function pipeToTokenResponse(injector: Injector) {
|
|
|
|
|
const storage = injector.get(OAuthStorage);
|
|
|
|
|
|
|
|
|
|
return pipe(
|
|
|
|
|
tap((res: TokenResponse) => {
|
|
|
|
|
const { access_token, refresh_token, scope: grantedScopes, expires_in } = res;
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|