From a5e5a5b40799e7505557f7ebd764b9c563be0f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Fri, 11 Feb 2022 15:19:37 +0100 Subject: [PATCH] feat: resume at origin path after login Closes #168 --- .../page/auth-callback-page.component.ts | 15 +++++++++--- src/app/modules/auth/auth-guard.service.ts | 16 +++++++++++-- src/app/modules/auth/auth-helper.service.ts | 21 +++++++++++++++++ .../page/auth-callback-page.component.ts | 15 +++++++++--- .../profile/page/profile-page.component.ts | 23 +++++++++++++++---- 5 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts index bfe36521..43441d46 100644 --- a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts +++ b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts @@ -1,9 +1,10 @@ import {Component, OnInit, OnDestroy} from '@angular/core'; import {NavController} from '@ionic/angular'; import {Router} from '@angular/router'; -import {IAuthAction} from 'ionic-appauth'; +import {AuthActions, IAuthAction} from 'ionic-appauth'; import {Subscription} from 'rxjs'; import {DefaultAuthService} from '../../default-auth.service'; +import {AuthHelperService} from '../../auth-helper.service'; @Component({ selector: 'auth-callback', @@ -17,6 +18,7 @@ export class AuthCallbackPageComponent implements OnInit, OnDestroy { private auth: DefaultAuthService, private navCtrl: NavController, private router: Router, + private authHelper: AuthHelperService, ) {} ngOnInit() { @@ -28,7 +30,14 @@ export class AuthCallbackPageComponent implements OnInit, OnDestroy { this.sub.unsubscribe(); } - async postCallback(_action: IAuthAction) { - await this.navCtrl.navigateRoot('profile'); + async postCallback(action: IAuthAction) { + if (action.action === AuthActions.SignInSuccess) { + const originPath = await this.authHelper.getOriginPath(); + this.navCtrl.navigateRoot(originPath ?? 'profile'); + this.authHelper.deleteOriginPath(); + } + if (action.action === AuthActions.SignInFailed) { + this.navCtrl.navigateRoot('profile'); + } } } diff --git a/src/app/modules/auth/auth-guard.service.ts b/src/app/modules/auth/auth-guard.service.ts index 2d59c885..cdc9b43c 100644 --- a/src/app/modules/auth/auth-guard.service.ts +++ b/src/app/modules/auth/auth-guard.service.ts @@ -1,5 +1,10 @@ import {Injectable} from '@angular/core'; -import {CanActivate, Router, RouterStateSnapshot} from '@angular/router'; +import { + CanActivate, + NavigationExtras, + Router, + RouterStateSnapshot, +} from '@angular/router'; import {DefaultAuthService} from './default-auth.service'; import {PAIAAuthService} from './paia/paia-auth.service'; import {IAuthService} from 'ionic-appauth'; @@ -33,7 +38,14 @@ export class AuthGuardService implements CanActivate { try { await this.authService.getValidToken(); } catch { - this.router.navigate(['profile']); + const originNavigation = this.router.getCurrentNavigation(); + let extras: NavigationExtras = {}; + if (originNavigation) { + const url = originNavigation.extractedUrl.toString(); + extras = {queryParams: {origin_path: url}}; + } + this.router.navigate(['profile'], extras); + return false; } diff --git a/src/app/modules/auth/auth-helper.service.ts b/src/app/modules/auth/auth-helper.service.ts index 00f6e9a7..b7b994e2 100644 --- a/src/app/modules/auth/auth-helper.service.ts +++ b/src/app/modules/auth/auth-helper.service.ts @@ -11,6 +11,8 @@ import { SCUserConfigurationMap, } from '@openstapps/core'; import {ConfigProvider} from '../config/config.provider'; +import {StorageProvider} from '../storage/storage.provider'; +const AUTH_ORIGIN_PATH = 'stapps.auth.origin_path'; @Injectable({ providedIn: 'root', @@ -21,6 +23,7 @@ export class AuthHelperService { constructor( private translateService: TranslateService, private configProvider: ConfigProvider, + private storageProvider: StorageProvider, ) { this.userConfigurationMap = ( this.configProvider.getAnyValue('auth') as { @@ -62,4 +65,22 @@ export class AuthHelperService { return user; } + + async deleteOriginPath() { + return this.storageProvider.delete(AUTH_ORIGIN_PATH); + } + + async setOriginPath(path: string) { + return this.storageProvider.put(AUTH_ORIGIN_PATH, path); + } + + async getOriginPath() { + let originPath: string; + try { + originPath = await this.storageProvider.get(AUTH_ORIGIN_PATH); + } catch { + return; + } + return originPath; + } } diff --git a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts b/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts index 70901f21..174b6dee 100644 --- a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts +++ b/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts @@ -1,9 +1,10 @@ import {Component, OnInit, OnDestroy} from '@angular/core'; import {NavController} from '@ionic/angular'; import {Router} from '@angular/router'; -import {IAuthAction} from 'ionic-appauth'; +import {AuthActions, IAuthAction} from 'ionic-appauth'; import {Subscription} from 'rxjs'; import {PAIAAuthService} from '../../paia-auth.service'; +import {AuthHelperService} from '../../../auth-helper.service'; @Component({ selector: 'auth-callback', @@ -17,6 +18,7 @@ export class PAIAAuthCallbackPageComponent implements OnInit, OnDestroy { private auth: PAIAAuthService, private navCtrl: NavController, private router: Router, + private authHelper: AuthHelperService, ) {} ngOnInit() { @@ -28,7 +30,14 @@ export class PAIAAuthCallbackPageComponent implements OnInit, OnDestroy { this.sub.unsubscribe(); } - async postCallback(_action: IAuthAction) { - this.navCtrl.navigateRoot('profile'); + async postCallback(action: IAuthAction) { + if (action.action === AuthActions.SignInSuccess) { + const originPath = await this.authHelper.getOriginPath(); + this.navCtrl.navigateRoot(originPath ?? 'profile'); + this.authHelper.deleteOriginPath(); + } + if (action.action === AuthActions.SignInFailed) { + this.navCtrl.navigateRoot('profile'); + } } } diff --git a/src/app/modules/profile/page/profile-page.component.ts b/src/app/modules/profile/page/profile-page.component.ts index d20c8974..cf1edef2 100644 --- a/src/app/modules/profile/page/profile-page.component.ts +++ b/src/app/modules/profile/page/profile-page.component.ts @@ -24,6 +24,7 @@ import { SCAuthorizationProviderType, SCUserConfiguration, } from '@openstapps/core'; +import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'app-home', @@ -36,6 +37,8 @@ export class ProfilePageComponent implements OnInit, OnDestroy { paia: {loggedIn: false}, }; + originPath: string | null; + userInfo?: SCUserConfiguration; subscriptions: Subscription[] = []; @@ -44,7 +47,8 @@ export class ProfilePageComponent implements OnInit, OnDestroy { private defaultAuth: DefaultAuthService, private paiaAuth: PAIAAuthService, private requestor: Requestor, - private authHelperService: AuthHelperService, + private authHelper: AuthHelperService, + private route: ActivatedRoute, ) {} ngOnInit() { @@ -70,6 +74,9 @@ export class ProfilePageComponent implements OnInit, OnDestroy { this.data.paia.loggedIn = false; }); }), + this.route.queryParamMap.subscribe(queryParams => { + this.originPath = queryParams.get('origin_path'); + }), ); } @@ -79,15 +86,17 @@ export class ProfilePageComponent implements OnInit, OnDestroy { userInfoHandler .performUserInfoRequest(this.defaultAuth.localConfiguration, token) .then(userInfo => { - this.userInfo = this.authHelperService.getUserFromUserInfo(userInfo); + this.userInfo = this.authHelper.getUserFromUserInfo(userInfo); }); } - public signIn() { + async signIn() { + await this.handleOriginPath(); this.defaultAuth.signIn(); } - signInPAIA() { + async signInPAIA() { + await this.handleOriginPath(); this.paiaAuth.signIn(); } @@ -105,4 +114,10 @@ export class ProfilePageComponent implements OnInit, OnDestroy { subscription.unsubscribe(); } } + + private async handleOriginPath() { + this.originPath + ? await this.authHelper.setOriginPath(this.originPath) + : await this.authHelper.deleteOriginPath(); + } }