From 0abbcdbee76366ce55f930316f5f229dad6b5ac9 Mon Sep 17 00:00:00 2001 From: Anthony Nahas Date: Mon, 8 Jul 2019 22:21:45 +0200 Subject: [PATCH] feat(package): added authGuardLoggedInURL to the config #263 --- demo/angular.json | 2 +- demo/src/app/app.module.ts | 4 +- .../app/examples/examples-routing.module.ts | 7 +++ demo/src/app/examples/examples.component.html | 16 ++++++ demo/src/app/examples/examples.module.ts | 8 ++- .../guards-example.component.html | 45 ++++++++++++++++ .../guards-example.component.scss | 0 .../guards-example.component.ts | 31 +++++++++++ .../logged-in/logged-in.component.html | 54 +++++++++++++++++++ .../logged-in/logged-in.component.scss | 0 .../logged-in/logged-in.component.ts | 35 ++++++++++++ .../logged-out/logged-out.component.html | 26 +++++++++ .../logged-out/logged-out.component.scss | 0 .../logged-out/logged-out.component.ts | 31 +++++++++++ src/module/guards/logged-in.guard.ts | 15 ++++-- src/module/interfaces/config.interface.ts | 2 + 16 files changed, 268 insertions(+), 8 deletions(-) create mode 100644 demo/src/app/examples/guards-example/guards-example.component.html create mode 100644 demo/src/app/examples/guards-example/guards-example.component.scss create mode 100644 demo/src/app/examples/guards-example/guards-example.component.ts create mode 100644 demo/src/app/examples/guards-example/logged-in/logged-in.component.html create mode 100644 demo/src/app/examples/guards-example/logged-in/logged-in.component.scss create mode 100644 demo/src/app/examples/guards-example/logged-in/logged-in.component.ts create mode 100644 demo/src/app/examples/guards-example/logged-out/logged-out.component.html create mode 100644 demo/src/app/examples/guards-example/logged-out/logged-out.component.scss create mode 100644 demo/src/app/examples/guards-example/logged-out/logged-out.component.ts diff --git a/demo/angular.json b/demo/angular.json index 87616a97..f4a0dc95 100644 --- a/demo/angular.json +++ b/demo/angular.json @@ -149,7 +149,7 @@ } }, "ngx-auth-firebaseui-e2e": { - "root": "", + "root": "e2e", "sourceRoot": "", "projectType": "application", "architect": { diff --git a/demo/src/app/app.module.ts b/demo/src/app/app.module.ts index 9a0b1b08..d08f2221 100644 --- a/demo/src/app/app.module.ts +++ b/demo/src/app/app.module.ts @@ -57,7 +57,9 @@ export function createTranslateLoader(http: HttpClient) { { enableFirestoreSync: true, toastMessageOnAuthSuccess: true, - toastMessageOnAuthError: true + toastMessageOnAuthError: true, + authGuardFallbackURL: 'examples/logged-out', + authGuardLoggedInURL: 'examples/logged-in', }), TranslateModule.forRoot({ loader: { diff --git a/demo/src/app/examples/examples-routing.module.ts b/demo/src/app/examples/examples-routing.module.ts index 7733f0d6..b9de754a 100644 --- a/demo/src/app/examples/examples-routing.module.ts +++ b/demo/src/app/examples/examples-routing.module.ts @@ -11,6 +11,10 @@ import {GoBackURLComponent} from './go-back-url/go-back-url.component'; import {RegistrationComponent} from './registration/registration.component'; import {ResetPasswordComponent} from './reset-psasword/reset-password.component'; import {PasswordStrengthComponent} from './password-strength/password-strength.component'; +import {GuardsExampleComponent} from './guards-example/guards-example.component'; +import {LoggedInGuard} from 'ngx-auth-firebaseui'; +import {LoggedInComponent} from './guards-example/logged-in/logged-in.component'; +import {LoggedOutComponent} from './guards-example/logged-out/logged-out.component'; @NgModule({ imports: [RouterModule.forChild([ @@ -25,6 +29,9 @@ import {PasswordStrengthComponent} from './password-strength/password-strength.c {path: 'tabIndex', component: TabIndexComponent}, {path: 'gobackurl', component: GoBackURLComponent}, {path: 'password-strength', component: PasswordStrengthComponent}, + {path: 'logged-in', component: LoggedInComponent}, + {path: 'logged-out', component: LoggedOutComponent}, + {path: 'guards', component: GuardsExampleComponent, canActivate : [LoggedInGuard]}, ])], exports: [RouterModule] }) diff --git a/demo/src/app/examples/examples.component.html b/demo/src/app/examples/examples.component.html index 625824b0..9ed48c20 100644 --- a/demo/src/app/examples/examples.component.html +++ b/demo/src/app/examples/examples.component.html @@ -156,6 +156,22 @@

Examples

+ + +
+ +
+ + Explore the guards api + + See how to protect your route from unauthenticated users + + + +
diff --git a/demo/src/app/examples/examples.module.ts b/demo/src/app/examples/examples.module.ts index 165ecec0..013a67b3 100644 --- a/demo/src/app/examples/examples.module.ts +++ b/demo/src/app/examples/examples.module.ts @@ -14,6 +14,9 @@ import { GoBackURLComponent } from './go-back-url/go-back-url.component'; import { RegistrationComponent } from './registration/registration.component'; import { ResetPasswordComponent } from './reset-psasword/reset-password.component'; import {PasswordStrengthComponent} from './password-strength/password-strength.component'; +import {GuardsExampleComponent} from './guards-example/guards-example.component'; +import {LoggedInComponent} from './guards-example/logged-in/logged-in.component'; +import {LoggedOutComponent} from './guards-example/logged-out/logged-out.component'; @NgModule({ imports: [ @@ -34,7 +37,10 @@ import {PasswordStrengthComponent} from './password-strength/password-strength.c GoBackURLComponent, RegistrationComponent, ResetPasswordComponent, - PasswordStrengthComponent + PasswordStrengthComponent, + GuardsExampleComponent, + LoggedInComponent, + LoggedOutComponent ] }) export class ExamplesModule { diff --git a/demo/src/app/examples/guards-example/guards-example.component.html b/demo/src/app/examples/guards-example/guards-example.component.html new file mode 100644 index 00000000..668e1637 --- /dev/null +++ b/demo/src/app/examples/guards-example/guards-example.component.html @@ -0,0 +1,45 @@ +
+
+

Password Strength API - Config

+
+
+ + +
+ +
+ + + +
+
+ +
+
+ + + + +
+
+
+ + + + + + diff --git a/demo/src/app/examples/guards-example/guards-example.component.scss b/demo/src/app/examples/guards-example/guards-example.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/demo/src/app/examples/guards-example/guards-example.component.ts b/demo/src/app/examples/guards-example/guards-example.component.ts new file mode 100644 index 00000000..0d7aa69b --- /dev/null +++ b/demo/src/app/examples/guards-example/guards-example.component.ts @@ -0,0 +1,31 @@ +import {Component} from '@angular/core'; +import {MatSnackBar} from '@angular/material/snack-bar'; +import {ExampleBaseComponent} from '../example.abstract'; + +@Component({ + selector: 'app-password-strength', + templateUrl: 'guards-example.component.html', + styleUrls: ['guards-example.component.scss'] +}) +export class GuardsExampleComponent extends ExampleBaseComponent { + + example = ` + `; + + constructor(public snackBar: MatSnackBar) { + super(snackBar); + } + + onStrengthChanged($event: number) { + console.log('on strength changed: ', $event); + } + +} diff --git a/demo/src/app/examples/guards-example/logged-in/logged-in.component.html b/demo/src/app/examples/guards-example/logged-in/logged-in.component.html new file mode 100644 index 00000000..a9176f69 --- /dev/null +++ b/demo/src/app/examples/guards-example/logged-in/logged-in.component.html @@ -0,0 +1,54 @@ +
+
+

Yes!! you are logged in! Congrats

+
+
+ + +
+ +
+ + + add authGuardFallbackURL and authGuardLoggedInURL to the NgxAuthFirebaseUIModule + +
+
+ +
+
+
+ + + use the canActivate feature on your route + +
+
+ +
+
+ + + + +
+
+
+ + + + + + diff --git a/demo/src/app/examples/guards-example/logged-in/logged-in.component.scss b/demo/src/app/examples/guards-example/logged-in/logged-in.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/demo/src/app/examples/guards-example/logged-in/logged-in.component.ts b/demo/src/app/examples/guards-example/logged-in/logged-in.component.ts new file mode 100644 index 00000000..ae1a950c --- /dev/null +++ b/demo/src/app/examples/guards-example/logged-in/logged-in.component.ts @@ -0,0 +1,35 @@ +import {Component} from '@angular/core'; +import {MatSnackBar} from '@angular/material/snack-bar'; +import {ExampleBaseComponent} from '../../example.abstract'; + +@Component({ + selector: 'app-logged-in', + templateUrl: 'logged-in.component.html', + styleUrls: ['logged-in.component.scss'] +}) +export class LoggedInComponent extends ExampleBaseComponent { + + example = ` + import {NgxAuthFirebaseUIModule} from 'ngx-auth-firebaseui'; + + + NgxAuthFirebaseUIModule.forRoot(firebaseKey, firebaseAppNameFactory, + { + authGuardFallbackURL: 'examples/logged-out', + authGuardLoggedInURL: 'examples/logged-in', + }),`; + + example2 = ` + import {LoggedInGuard} from 'ngx-auth-firebaseui'; + + {path: 'guards', component: GuardsExampleComponent, canActivate : [LoggedInGuard]},`; + + constructor(public snackBar: MatSnackBar) { + super(snackBar); + } + + onStrengthChanged($event: number) { + console.log('on strength changed: ', $event); + } + +} diff --git a/demo/src/app/examples/guards-example/logged-out/logged-out.component.html b/demo/src/app/examples/guards-example/logged-out/logged-out.component.html new file mode 100644 index 00000000..1f015fca --- /dev/null +++ b/demo/src/app/examples/guards-example/logged-out/logged-out.component.html @@ -0,0 +1,26 @@ +
+
+

Sorry!! You are logged out

+
+
+ + +
+ +
+ + + Please log in + + + + + +
+
+ + + + + + diff --git a/demo/src/app/examples/guards-example/logged-out/logged-out.component.scss b/demo/src/app/examples/guards-example/logged-out/logged-out.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/demo/src/app/examples/guards-example/logged-out/logged-out.component.ts b/demo/src/app/examples/guards-example/logged-out/logged-out.component.ts new file mode 100644 index 00000000..55b99b00 --- /dev/null +++ b/demo/src/app/examples/guards-example/logged-out/logged-out.component.ts @@ -0,0 +1,31 @@ +import {Component} from '@angular/core'; +import {MatSnackBar} from '@angular/material/snack-bar'; +import {ExampleBaseComponent} from '../../example.abstract'; + +@Component({ + selector: 'app-logged-out', + templateUrl: 'logged-out.component.html', + styleUrls: ['logged-out.component.scss'] +}) +export class LoggedOutComponent extends ExampleBaseComponent { + + example = ` + `; + + constructor(public snackBar: MatSnackBar) { + super(snackBar); + } + + onStrengthChanged($event: number) { + console.log('on strength changed: ', $event); + } + +} diff --git a/src/module/guards/logged-in.guard.ts b/src/module/guards/logged-in.guard.ts index 22a8a8c9..f3b06b0d 100644 --- a/src/module/guards/logged-in.guard.ts +++ b/src/module/guards/logged-in.guard.ts @@ -1,7 +1,7 @@ -import { Inject, Injectable } from '@angular/core' -import { CanActivate, Router } from '@angular/router' -import { map } from 'rxjs/operators' -import { Observable } from 'rxjs' +import {Inject, Injectable} from '@angular/core' +import {CanActivate, Router} from '@angular/router' +import {map} from 'rxjs/operators' +import {Observable} from 'rxjs' import { AuthProcessService, @@ -25,9 +25,14 @@ export class LoggedInGuard implements CanActivate { return this.auth.afa.user.pipe( map(res => { if (res) { + if (this.config.authGuardLoggedInURL) { + this.router.navigate([`/${this.config.authGuardLoggedInURL}`]); + } return true } - this.router.navigate([`/${this.config.authGuardFallbackURL}`]); + if (this.config.authGuardFallbackURL) { + this.router.navigate([`/${this.config.authGuardFallbackURL}`]); + } return false }), ) diff --git a/src/module/interfaces/config.interface.ts b/src/module/interfaces/config.interface.ts index a7eecf14..b5044faa 100644 --- a/src/module/interfaces/config.interface.ts +++ b/src/module/interfaces/config.interface.ts @@ -12,6 +12,7 @@ export interface NgxAuthFirebaseUIConfig { // authProviders?: Array, // languageCode?: string, // todo: 28.3.18 authGuardFallbackURL?: string, + authGuardLoggedInURL?: string, enableFirestoreSync?: boolean, toastMessageOnAuthSuccess?: boolean, toastMessageOnAuthError?: boolean @@ -21,6 +22,7 @@ export const defaultAuthFirebaseUIConfig: NgxAuthFirebaseUIConfig = { // authMethod: 'redirect', // authProviders: [new GoogleAuthProvider(), new FacebookAuthProvider(), new TwitterAuthProvider(), new GithubAuthProvider()], authGuardFallbackURL: '/', + authGuardLoggedInURL: '/', enableFirestoreSync: true, toastMessageOnAuthSuccess: true, toastMessageOnAuthError: true