Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remember me implementation #18768

Merged
merged 11 commits into from
Jan 29, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AbpLocalStorageService } from "@abp/ng.core";
import { Injectable, Injector } from "@angular/core";

const rememberMe = 'remember_me';

@Injectable({
providedIn: 'root'
})
export class RememberMeService {
constructor(private injector: Injector) { }
localStorageService = this.injector.get(AbpLocalStorageService);
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved

set(remember: boolean) {
this.localStorageService.setItem(rememberMe, JSON.stringify(remember));
}

remove() {
this.localStorageService.removeItem(rememberMe);
}

get() {
return Boolean(JSON.parse(this.localStorageService.getItem(rememberMe)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { noop } from '@abp/ng.core';
import { Params } from '@angular/router';
import { from, of } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { RememberMeService, isTokenExpired } from '../utils';
import { isTokenExpired } from '../utils';

export class AuthCodeFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = false;
private rememberMe = 'remember_me'
rememberMeService = new RememberMeService(this.injector);
readonly #rememberMe = 'remember_me'
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved

async init() {
this.checkRememberMeOption();
Expand All @@ -21,22 +20,22 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption() {
const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService);
let rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));
let rememberMe = this.rememberMeService.get();

if (accessToken && !rememberMe) {
let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));
const rememberMeValue = Boolean(parsedToken[this.rememberMe]);
const rememberMeValue = Boolean(parsedToken[this.#rememberMe]);

if (rememberMeValue) {
this.rememberMeService.setRememberMe(true);
this.rememberMeService.set(true);
} else {
this.rememberMeService.setRememberMe(false)
this.rememberMeService.set(false)
}
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved
}

rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));
rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.oAuthService.logOut();
}
}
Expand All @@ -57,7 +56,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
}

logout(queryParams?: Params) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { clearOAuthStorage } from '../utils/clear-o-auth-storage';
import { oAuthStorage } from '../utils/oauth-storage';
import { OAuthErrorFilterService } from '../services';
import { isTokenExpired } from '../utils';
import { RememberMeService } from '../services/remember-me.service';

export abstract class AuthFlowStrategy {
abstract readonly isInternalAuth: boolean;
Expand All @@ -36,6 +37,7 @@ export abstract class AuthFlowStrategy {
protected oAuthConfig!: AuthConfig;
protected sessionState: SessionStateService;
protected localStorageService: AbpLocalStorageService;
protected rememberMeService: RememberMeService;
protected tenantKey: string;
protected router: Router;

Expand All @@ -62,6 +64,7 @@ export abstract class AuthFlowStrategy {
this.tenantKey = injector.get(TENANT_KEY);
this.router = injector.get(Router);
this.oAuthErrorFilterService = injector.get(OAuthErrorFilterService);
this.rememberMeService = injector.get(RememberMeService);

this.listenToOauthErrors();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { Params, Router } from '@angular/router';
import { from, Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { RememberMeService, isTokenExpired, pipeToLogin } from '../utils/auth-utils';
import { isTokenExpired, pipeToLogin } from '../utils/auth-utils';
import { AbpLocalStorageService, LoginParams } from '@abp/ng.core';
import { clearOAuthStorage } from '../utils/clear-o-auth-storage';

export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = true;
rememberMeService = new RememberMeService(this.injector);

private listenToTokenExpiration() {
this.oAuthService.events
Expand All @@ -26,7 +25,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
this.refreshToken();
} else {
this.oAuthService.logOut();
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.configState.refreshAppState().subscribe();
}
});
Expand All @@ -41,9 +40,9 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption(localStorageService: AbpLocalStorageService) {
const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService);
const rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()))
const rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.oAuthService.logOut();
}
}
Expand Down Expand Up @@ -76,15 +75,15 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
switchMap(() => this.configState.refreshAppState()),
tap(() => {
router.navigateByUrl('/');
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved
}),
);
}

protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => {
clearOAuthStorage();
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
});
}
}
25 changes: 3 additions & 22 deletions npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,21 @@ import {
ConfigStateService,
LoginParams,
PipeToLoginFn,
AbpLocalStorageService,
} from '@abp/ng.core';
import { OAuthService } from 'angular-oauth2-oidc';

const rememberMe = 'remember_me';

export class RememberMeService {
constructor(private injector: Injector) { }
localStorageService = this.injector.get(AbpLocalStorageService);

setRememberMe(remember: boolean) {
this.localStorageService.setItem(rememberMe, JSON.stringify(remember));
}

removeRememberMe() {
this.localStorageService.removeItem(rememberMe);
}

getRememberMe() {
return this.localStorageService.getItem(rememberMe);
}
}
import { RememberMeService } from '../services/remember-me.service';

export const pipeToLogin: PipeToLoginFn = function (
params: Pick<LoginParams, 'redirectUrl' | 'rememberMe'>,
injector: Injector,
) {
const configState = injector.get(ConfigStateService);
const router = injector.get(Router);
const rememberMeService = new RememberMeService(injector);
const rememberMeService = injector.get(RememberMeService);
return pipe(
switchMap(() => configState.refreshAppState()),
tap(() => {
rememberMeService.setRememberMe(params.rememberMe);
rememberMeService.set(params.rememberMe);
if (params.redirectUrl) router.navigate([params.redirectUrl]);
}),
);
Expand Down
Loading