| <router-outlet></router-outlet> | <router-outlet></router-outlet> | ||||
| <div class="vimeo-wrapper"> | |||||
| <!-- <div class="vimeo-wrapper"> | |||||
| <iframe id=bgVideo src="https://player.vimeo.com/video/468031372?background=1&controls=0" frameborder="0" | <iframe id=bgVideo src="https://player.vimeo.com/video/468031372?background=1&controls=0" frameborder="0" | ||||
| allow="autoplay; fullscreen" allowfullscreen></iframe> | allow="autoplay; fullscreen" allowfullscreen></iframe> | ||||
| <div class="overlay"> | |||||
| </div> | |||||
| </div> | |||||
| <div class="overlay"></div> | |||||
| </div> --> |
| ngOnInit (){ | ngOnInit (){ | ||||
| this.loginSub = this.authService.loginSuccess.subscribe( | 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; | |||||
| } | } | ||||
| ) | ) | ||||
| } | } |
| <div class='parent'> | <div class='parent'> | ||||
| <div class="form_login"> | <div class="form_login"> | ||||
| <form class="k-form " [formGroup]="userForm"> | |||||
| <form *ngIf='!loading' class="k-form " [formGroup]="userForm"> | |||||
| <fieldset class="k-form-fieldset"> | <fieldset class="k-form-fieldset"> | ||||
| <legend class="k-form-legend">Login to Supercredit</legend> | <legend class="k-form-legend">Login to Supercredit</legend> | ||||
| <kendo-formfield [showErrors]="'initial'"> | <kendo-formfield [showErrors]="'initial'"> | ||||
| </div> | </div> | ||||
| </fieldset> | </fieldset> | ||||
| </form> | </form> | ||||
| <div *ngIf='loading' style="min-height: 200px;"> auth loading...</div> | |||||
| </div> | </div> | ||||
| </div> | </div> |
| import { Component, OnInit } from '@angular/core'; | |||||
| import { Component, OnDestroy, OnInit } from '@angular/core'; | |||||
| import { FormControl, FormGroup, Validators } from '@angular/forms'; | import { FormControl, FormGroup, Validators } from '@angular/forms'; | ||||
| import { Router } from '@angular/router'; | import { Router } from '@angular/router'; | ||||
| import { Subscription } from 'rxjs'; | |||||
| import { AuthService } from '../service/auth.service'; | import { AuthService } from '../service/auth.service'; | ||||
| templateUrl: './auth.component.html', | templateUrl: './auth.component.html', | ||||
| styleUrls: ['./auth.component.scss'] | 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({ | public userForm: FormGroup = new FormGroup({ | ||||
| password: new FormControl('abcdefg', [Validators.minLength(3), Validators.maxLength(20)]), | password: new FormControl('abcdefg', [Validators.minLength(3), Validators.maxLength(20)]), | ||||
| constructor(private authService: AuthService, private rounter: Router) { } | constructor(private authService: AuthService, private rounter: Router) { } | ||||
| ngOnInit(): void { | 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() { | submitForm() { | ||||
| console.log(this.userForm); | 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.prototype.ngOnInit = 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 () { | AuthComponent.prototype.submitForm = function () { | ||||
| console.log(this.userForm); | 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([ | AuthComponent = __decorate([ | ||||
| core_1.Component({ | core_1.Component({ |
| @Injectable() | @Injectable() | ||||
| export class AuthGuard implements CanActivate, CanActivateChild { | export class AuthGuard implements CanActivate, CanActivateChild { | ||||
| constructor(private authService: AuthService, private router: Router) {} | |||||
| constructor(private authService: AuthService, private router: Router) { } | |||||
| canActivate(route: ActivatedRouteSnapshot, | canActivate(route: ActivatedRouteSnapshot, | ||||
| state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |||||
| return this.authService.isAuthenticated() | |||||
| .then( | |||||
| (authenticated: boolean) => { | |||||
| if (authenticated) { | |||||
| return true; | |||||
| } else { | |||||
| this.router.navigate(['/login']); | |||||
| } | |||||
| } | |||||
| ); | |||||
| state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |||||
| if (this.authService.isAuthenticated()) { | |||||
| return true; | |||||
| } else { | |||||
| this.router.navigate(['/login']); | |||||
| } | |||||
| } | } | ||||
| canActivateChild(route: ActivatedRouteSnapshot, | canActivateChild(route: ActivatedRouteSnapshot, | ||||
| state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |||||
| state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |||||
| return this.canActivate(route, state); | return this.canActivate(route, state); | ||||
| } | } | ||||
| } | } |
| @Injectable() | @Injectable() | ||||
| export class AuthService { | export class AuthService { | ||||
| loggedIn: boolean = false; | |||||
| private loggedIn: boolean = false; | |||||
| loginSuccess = new EventEmitter <any>(); | loginSuccess = new EventEmitter <any>(); | ||||
| isAuthenticated() { | 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() { | logout() { |
| this.router = router; | this.router = router; | ||||
| } | } | ||||
| AuthGuard.prototype.canActivate = function (route, state) { | 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) { | AuthGuard.prototype.canActivateChild = function (route, state) { | ||||
| return this.canActivate(route, state); | return this.canActivate(route, state); |
| this.loginSuccess = new core_1.EventEmitter(); | this.loginSuccess = new core_1.EventEmitter(); | ||||
| } | } | ||||
| AuthService.prototype.isAuthenticated = function () { | AuthService.prototype.isAuthenticated = function () { | ||||
| return this.loggedIn; | |||||
| }; | |||||
| AuthService.prototype.login = function (email, password) { | |||||
| var _this = this; | var _this = this; | ||||
| var promise = new Promise(function (resolve, reject) { | var promise = new Promise(function (resolve, reject) { | ||||
| setTimeout(function () { | setTimeout(function () { | ||||
| resolve(_this.loggedIn); | 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); | }, 800); | ||||
| }); | }); | ||||
| return promise; | return promise; | ||||
| }; | }; | ||||
| AuthService.prototype.login = function (emai, password) { | |||||
| this.loggedIn = true; | |||||
| }; | |||||
| AuthService.prototype.logout = function () { | AuthService.prototype.logout = function () { | ||||
| this.loggedIn = false; | this.loggedIn = false; | ||||
| }; | }; |