From 34fa7c9bdd2e98770673cf1113db00e216e16505 Mon Sep 17 00:00:00 2001 From: Patrick Sun Date: Sun, 22 Nov 2020 17:33:33 +1100 Subject: [PATCH] loggin service now has a mimic on login delay simulation with loading indicator. --- src/app/app.component.html | 10 ++++---- src/app/app.component.ts | 8 ++----- src/app/auth/auth.component.html | 4 +++- src/app/auth/auth.component.ts | 28 +++++++++++++++++----- src/app/auth/dist/auth.component.js | 18 ++++++++++---- src/app/service/auth-guard.service.ts | 21 +++++++--------- src/app/service/auth.service.ts | 28 +++++++++++++--------- src/app/service/dist/auth-guard.service.js | 16 +++++-------- src/app/service/dist/auth.service.js | 10 +++++--- 9 files changed, 83 insertions(+), 60 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 82ba9be..b7b8275 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -27,11 +27,9 @@ -
+ \ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index b7da0b7..ea9a102 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -48,12 +48,8 @@ export class AppComponent implements OnInit , OnDestroy { ngOnInit (){ this.loginSub = this.authService.loginSuccess.subscribe( - (item: any) => { - console.log(item); - if (item != null) - this.login = true; - else - this.login = false; + (success: boolean) => { + this.login = success; } ) } diff --git a/src/app/auth/auth.component.html b/src/app/auth/auth.component.html index b9ad505..334f72f 100644 --- a/src/app/auth/auth.component.html +++ b/src/app/auth/auth.component.html @@ -1,7 +1,7 @@
+ +
auth loading...
\ No newline at end of file diff --git a/src/app/auth/auth.component.ts b/src/app/auth/auth.component.ts index 5576fd4..31083ba 100644 --- a/src/app/auth/auth.component.ts +++ b/src/app/auth/auth.component.ts @@ -1,6 +1,7 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { Router } from '@angular/router'; +import { Subscription } from 'rxjs'; import { AuthService } from '../service/auth.service'; @@ -9,7 +10,9 @@ import { AuthService } from '../service/auth.service'; templateUrl: './auth.component.html', styleUrls: ['./auth.component.scss'] }) -export class AuthComponent implements OnInit { +export class AuthComponent implements OnInit, OnDestroy{ + loading : boolean ; // a state that user is currently loading loggin + loginSub: Subscription; public userForm: FormGroup = new FormGroup({ password: new FormControl('abcdefg', [Validators.minLength(3), Validators.maxLength(20)]), @@ -19,13 +22,26 @@ export class AuthComponent implements OnInit { constructor(private authService: AuthService, private rounter: Router) { } ngOnInit(): void { - this.authService.loginSuccess.emit(null); + this.loginSub = this.authService.loginSuccess.subscribe( + (ok :boolean ) =>{ + this.loading = false + console.log (" found login success " + ok ); + if (ok) + this.rounter.navigate(["/dashboard"]); + else + alert('fuck this is wrong'); + } + ); + } + + ngOnDestroy(): void { + this.loginSub.unsubscribe(); } submitForm() { console.log(this.userForm); - this.authService.login("email", "pass"); - this.authService.loginSuccess.emit(this.userForm) - this.rounter.navigate(["/dashboard"]); + this.loading = true; + this.authService.login(this.userForm.value.email, this.userForm.value.password); + } } diff --git a/src/app/auth/dist/auth.component.js b/src/app/auth/dist/auth.component.js index 00110d7..06f7c70 100644 --- a/src/app/auth/dist/auth.component.js +++ b/src/app/auth/dist/auth.component.js @@ -19,13 +19,23 @@ var AuthComponent = /** @class */ (function () { }); } AuthComponent.prototype.ngOnInit = function () { - this.authService.loginSuccess.emit(null); + var _this = this; + this.loginSub = this.authService.loginSuccess.subscribe(function (ok) { + _this.loading = false; + console.log(" found login success " + ok); + if (ok) + _this.rounter.navigate(["/dashboard"]); + else + alert('fuck this is wrong'); + }); + }; + AuthComponent.prototype.ngOnDestroy = function () { + this.loginSub.unsubscribe(); }; AuthComponent.prototype.submitForm = function () { console.log(this.userForm); - this.authService.login("email", "pass"); - this.authService.loginSuccess.emit(this.userForm); - this.rounter.navigate(["/dashboard"]); + this.loading = true; + this.authService.login(this.userForm.value.email, this.userForm.value.password); }; AuthComponent = __decorate([ core_1.Component({ diff --git a/src/app/service/auth-guard.service.ts b/src/app/service/auth-guard.service.ts index 931bcb1..3959572 100644 --- a/src/app/service/auth-guard.service.ts +++ b/src/app/service/auth-guard.service.ts @@ -12,24 +12,19 @@ import { AuthService } from './auth.service'; @Injectable() export class AuthGuard implements CanActivate, CanActivateChild { - constructor(private authService: AuthService, private router: Router) {} + constructor(private authService: AuthService, private router: Router) { } canActivate(route: ActivatedRouteSnapshot, - state: RouterStateSnapshot): Observable | Promise | boolean { - return this.authService.isAuthenticated() - .then( - (authenticated: boolean) => { - if (authenticated) { - return true; - } else { - this.router.navigate(['/login']); - } - } - ); + state: RouterStateSnapshot): Observable | Promise | boolean { + if (this.authService.isAuthenticated()) { + return true; + } else { + this.router.navigate(['/login']); + } } canActivateChild(route: ActivatedRouteSnapshot, - state: RouterStateSnapshot): Observable | Promise | boolean { + state: RouterStateSnapshot): Observable | Promise | boolean { return this.canActivate(route, state); } } diff --git a/src/app/service/auth.service.ts b/src/app/service/auth.service.ts index 7e2c624..b8b517c 100644 --- a/src/app/service/auth.service.ts +++ b/src/app/service/auth.service.ts @@ -3,22 +3,28 @@ import { BooleanFilterComponent } from '@progress/kendo-angular-grid'; @Injectable() export class AuthService { - loggedIn: boolean = false; + private loggedIn: boolean = false; loginSuccess = new EventEmitter (); isAuthenticated() { - const promise = new Promise( - (resolve, reject) => { - setTimeout(() => { - resolve(this.loggedIn); - }, 800); - } - ); - return promise; + return this.loggedIn; } - login(emai: string, password: string) { - this.loggedIn = true; + login(email: string, password: string) { + + const promise = new Promise( + (resolve, reject) => { + setTimeout(() => { + resolve(this.loggedIn); + console.log("email = " + email + " password = " + password); + var result = (( email == "email@email.com" ) && (password == "password")); + this.loggedIn = result; + this.loginSuccess.emit(result); + }, 800); + } + ); + return promise; + } logout() { diff --git a/src/app/service/dist/auth-guard.service.js b/src/app/service/dist/auth-guard.service.js index c3dfc54..5babecd 100644 --- a/src/app/service/dist/auth-guard.service.js +++ b/src/app/service/dist/auth-guard.service.js @@ -14,16 +14,12 @@ var AuthGuard = /** @class */ (function () { this.router = router; } AuthGuard.prototype.canActivate = function (route, state) { - var _this = this; - return this.authService.isAuthenticated() - .then(function (authenticated) { - if (authenticated) { - return true; - } - else { - _this.router.navigate(['/login']); - } - }); + if (this.authService.isAuthenticated()) { + return true; + } + else { + this.router.navigate(['/login']); + } }; AuthGuard.prototype.canActivateChild = function (route, state) { return this.canActivate(route, state); diff --git a/src/app/service/dist/auth.service.js b/src/app/service/dist/auth.service.js index bfeff6e..74bc084 100644 --- a/src/app/service/dist/auth.service.js +++ b/src/app/service/dist/auth.service.js @@ -14,17 +14,21 @@ var AuthService = /** @class */ (function () { this.loginSuccess = new core_1.EventEmitter(); } AuthService.prototype.isAuthenticated = function () { + return this.loggedIn; + }; + AuthService.prototype.login = function (email, password) { var _this = this; var promise = new Promise(function (resolve, reject) { setTimeout(function () { resolve(_this.loggedIn); + console.log("email = " + email + " password = " + password); + var result = ((email == "email@email.com") && (password == "password")); + _this.loggedIn = result; + _this.loginSuccess.emit(result); }, 800); }); return promise; }; - AuthService.prototype.login = function (emai, password) { - this.loggedIn = true; - }; AuthService.prototype.logout = function () { this.loggedIn = false; };