Guest User

Untitled

a guest
Sep 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router, Route ,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, CanLoad } from '@angular/router';
  3. import { Observable } from 'rxjs/Observable';
  4. import { SimpleAuthService } from './simple-auth.service';
  5.  
  6. @Injectable()
  7. export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
  8. constructor(private authService: SimpleAuthService) { }
  9.  
  10. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  11. const url: string = state.url;
  12. return this.checkLogin(url);
  13. }
  14.  
  15. canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  16. return this.canActivate(route, state);
  17. }
  18.  
  19. canLoad(route: Route): Observable<boolean> {
  20. const url: string = route.path;
  21. return this.checkLogin(url);
  22. }
  23.  
  24. checkLogin(url: string): Observable<boolean> {
  25. return this.authService.isLoggedIn(url);
  26. }
  27. }
  28.  
  29. import { Component, OnInit, ElementRef } from '@angular/core';
  30. import { Router, ActivatedRoute } from '@angular/router';
  31. import { SimpleAuthService } from '../../core/simple-auth.service';
  32.  
  33. declare var $: any;
  34.  
  35. @Component({
  36. selector: 'app-login-cmp',
  37. templateUrl: './login.component.html'
  38. })
  39.  
  40. export class LoginComponent implements OnInit {
  41. private toggleButton: any;
  42. private sidebarVisible: boolean;
  43. private nativeElement: Node;
  44. private email ='user@dev.org'
  45. private password ='useruser';
  46.  
  47.  
  48. constructor(private element: ElementRef, public authService: SimpleAuthService,
  49. private router: Router, private route: ActivatedRoute) {
  50. if (this.authService.login(this.email,this.password)) {
  51. this.router.navigate(['dashboard']);
  52. } else {
  53. this.nativeElement = element.nativeElement;
  54. this.sidebarVisible = false;
  55. }
  56. }
  57.  
  58. ngOnInit() {
  59. this.login(this.email, this.password);
  60. var navbar : HTMLElement = this.element.nativeElement;
  61. this.toggleButton = navbar.getElementsByClassName('navbar-toggle')[0];
  62.  
  63. setTimeout(function() {
  64. // after 1000 ms we add the class animated to the login/register card
  65. $('.card').removeClass('card-hidden');
  66. }, 700);
  67. }
  68. sidebarToggle() {
  69. var toggleButton = this.toggleButton;
  70. var body = document.getElementsByTagName('body')[0];
  71. var sidebar = document.getElementsByClassName('navbar-collapse')[0];
  72. if (this.sidebarVisible == false) {
  73. setTimeout(function() {
  74. toggleButton.classList.add('toggled');
  75. }, 500);
  76. body.classList.add('nav-open');
  77. this.sidebarVisible = true;
  78. } else {
  79. this.toggleButton.classList.remove('toggled');
  80. this.sidebarVisible = false;
  81. body.classList.remove('nav-open');
  82. }
  83. }
  84.  
  85. login(username: string, password: string): void {
  86. this.authService.login(username, password).then(_ => {
  87. const redirectUrl: string = this.authService.redirectUrl || 'dashboard';
  88. this.router.navigate([redirectUrl]);
  89. });
  90. }
  91. }
  92.  
  93. import { Injectable } from '@angular/core';
  94. import { Router, Route ,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, CanLoad } from '@angular/router';
  95. import { Observable } from 'rxjs/Observable';
  96. import { AngularFireAuth } from 'angularfire2/auth';
  97. import { User, UserCredential } from '@firebase/auth-types';
  98. import { take, map, tap } from 'rxjs/operators';
  99.  
  100. @Injectable()
  101. export class SimpleAuthService {
  102. user: Observable<User>;
  103. redirectUrl: string;
  104.  
  105. constructor(private afAuth: AngularFireAuth, private router: Router) {
  106. this.user = this.afAuth.authState;
  107. }
  108.  
  109. getUser(): Observable<User> {
  110. return this.user.pipe(take(1));
  111. }
  112.  
  113. isLoggedIn(redirectUrl: string): Observable<boolean> {
  114. return this.user.pipe(
  115. take(1),
  116. map(authState => !!authState),
  117. tap(authenticated => {
  118. if (!authenticated) {
  119. this.redirectUrl = redirectUrl;
  120. this.router.navigate(['/']);
  121. }
  122. })
  123. );
  124. }
  125.  
  126. login(username: string, password: string): Promise<UserCredential> {
  127. return this.afAuth.auth.signInWithEmailAndPassword(username, password);
  128. }
  129.  
  130. logout(): Promise<boolean> {
  131. return this.afAuth.auth.signOut().then(() => this.router.navigate(['/login']));
  132. }
  133. }
  134.  
  135. function login(): Promise<boolean> {
  136. return Promise.resolve(true);
  137. }
  138.  
  139. if(login()) {} // this is always true regardless of `catch()` is triggered sometime in the future
  140.  
  141. import { AngularFireAuth } from 'angularfire2/auth';
  142. import { User, UserCredential } from '@firebase/auth-types';
  143.  
  144. @Injectable()
  145. export class AuthService {
  146. user: Observable<User>;
  147. redirectUrl: string;
  148.  
  149. constructor(private afAuth: AngularFireAuth, private router: Router) {
  150. this.user = this.afAuth.authState;
  151. }
  152.  
  153. getUser(): Observable<User> {
  154. return this.user.pipe(take(1));
  155. }
  156.  
  157. isLoggedIn(redirectUrl: string): Observable<boolean> {
  158. return this.user.pipe(
  159. take(1),
  160. map(authState => !!authState),
  161. tap(authenticated => {
  162. if (!authenticated) {
  163. this.redirectUrl = redirectUrl;
  164. this.router.navigate(['/']);
  165. }
  166. })
  167. );
  168. }
  169.  
  170. login(username: string, password: string): Promise<UserCredential> {
  171. return this.afAuth.auth.signInWithEmailAndPassword(email, password);
  172. }
  173.  
  174. logout(): Promise<boolean> {
  175. return this.afAuth.auth.signOut().then(() => this.router.navigate(['/login']));
  176. }
  177. }
  178.  
  179. @Injectable()
  180. export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
  181. constructor(private authService: AuthService) { }
  182.  
  183. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  184. const url: string = state.url;
  185. return this.checkLogin(url);
  186. }
  187.  
  188. canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  189. return this.canActivate(route, state);
  190. }
  191.  
  192. canLoad(route: Route): Observable<boolean> {
  193. const url: string = route.path;
  194. return this.checkLogin(url);
  195. }
  196.  
  197. checkLogin(url: string): Observable<boolean> {
  198. return this.authService.isLoggedIn(url);
  199. }
  200. }
  201.  
  202. export class LoginComponent implements OnInit {
  203. constructor(private router: Router, public authService: AuthService) { }
  204.  
  205. ngOnInit() {
  206. this.login(this.email, this.password);
  207. }
  208.  
  209. login(username: string, password: string): void {
  210. this.authService.login(username, password).then(_ => {
  211. const redirectUrl: string = this.authService.redirectUrl || '/some-default-route';
  212. this.router.navigate([redirectUrl]);
  213. });
  214. }
  215. }
Add Comment
Please, Sign In to add comment