Guest User

Untitled

a guest
Sep 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import { Routes } from '@angular/router';
  2. import { SignUpComponent } from './app/login/sign-up/sign-up.component';
  3. import { SignInComponent } from './app/login/sign-in/sign-in.component';
  4. import { ChatRoomComponent } from './app/layout/chat-room/chat-room.component';
  5. import { PreventLoggedInAccess } from './app/shared/guards/prevent-logged-in.access';
  6. import { AuthGuard } from './app/shared/guards/auth.guard';
  7.  
  8. export const appRoutes: Routes = [
  9. { path: '', redirectTo: 'chat', pathMatch: 'full'},
  10. { path: 'sign-up', component: SignUpComponent, canActivate: [PreventLoggedInAccess] },
  11. { path: 'sign-in', component: SignInComponent, canActivate: [PreventLoggedInAccess] },
  12. { path: 'chat', component: ChatRoomComponent, canActivate: [AuthGuard] }
  13. ];
  14.  
  15. import { Injectable } from '@angular/core';
  16. import { CanActivate, Router } from "@angular/router";
  17. import { AuthService } from "../../services/auth.service";
  18.  
  19. @Injectable()
  20.  
  21. export class AuthGuard implements CanActivate {
  22.  
  23. constructor(private authService:AuthService,
  24. private router:Router){}
  25.  
  26. canActivate() {
  27. if(this.authService.authUser()){
  28. return true;
  29. }else{
  30. this.router.navigate(['/sign-in']);
  31. return false;
  32. }
  33. }
  34. }
  35.  
  36. import { Injectable } from '@angular/core';
  37. import { CanActivate } from '@angular/router';
  38. import { AuthService } from "../../services/auth.service";
  39.  
  40. @Injectable()
  41.  
  42. export class PreventLoggedInAccess implements CanActivate {
  43.  
  44. constructor(
  45. private authService:AuthService
  46. ) {}
  47.  
  48. canActivate() {
  49. return !this.authService.authUser();
  50. }
  51. }
  52.  
  53. import { Injectable } from '@angular/core';
  54. import { Router } from '@angular/router';
  55. import { AngularFireAuth } from 'angularfire2/auth';
  56. import { AngularFireDatabase } from 'angularfire2/database';
  57. import * as firebase from 'firebase/app';
  58. import { Observable } from 'rxjs/Observable';
  59. import { User } from '../models/user.model';
  60.  
  61. @Injectable()
  62.  
  63. export class AuthService {
  64. private user: Observable<firebase.User>;
  65. private authState: any;
  66.  
  67. constructor(private dbAuth: AngularFireAuth,
  68. private db: AngularFireDatabase,
  69. private router: Router) {
  70. this.user = dbAuth.authState;
  71. }
  72.  
  73. authUser() {
  74. return this.user;
  75. }
  76. ....
  77.  
  78. }
Add Comment
Please, Sign In to add comment