Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Routes } from '@angular/router';
- import { SignUpComponent } from './app/login/sign-up/sign-up.component';
- import { SignInComponent } from './app/login/sign-in/sign-in.component';
- import { ChatRoomComponent } from './app/layout/chat-room/chat-room.component';
- import { PreventLoggedInAccess } from './app/shared/guards/prevent-logged-in.access';
- import { AuthGuard } from './app/shared/guards/auth.guard';
- export const appRoutes: Routes = [
- { path: '', redirectTo: 'chat', pathMatch: 'full'},
- { path: 'sign-up', component: SignUpComponent, canActivate: [PreventLoggedInAccess] },
- { path: 'sign-in', component: SignInComponent, canActivate: [PreventLoggedInAccess] },
- { path: 'chat', component: ChatRoomComponent, canActivate: [AuthGuard] }
- ];
- import { Injectable } from '@angular/core';
- import { CanActivate, Router } from "@angular/router";
- import { AuthService } from "../../services/auth.service";
- @Injectable()
- export class AuthGuard implements CanActivate {
- constructor(private authService:AuthService,
- private router:Router){}
- canActivate() {
- if(this.authService.authUser()){
- return true;
- }else{
- this.router.navigate(['/sign-in']);
- return false;
- }
- }
- }
- import { Injectable } from '@angular/core';
- import { CanActivate } from '@angular/router';
- import { AuthService } from "../../services/auth.service";
- @Injectable()
- export class PreventLoggedInAccess implements CanActivate {
- constructor(
- private authService:AuthService
- ) {}
- canActivate() {
- return !this.authService.authUser();
- }
- }
- import { Injectable } from '@angular/core';
- import { Router } from '@angular/router';
- import { AngularFireAuth } from 'angularfire2/auth';
- import { AngularFireDatabase } from 'angularfire2/database';
- import * as firebase from 'firebase/app';
- import { Observable } from 'rxjs/Observable';
- import { User } from '../models/user.model';
- @Injectable()
- export class AuthService {
- private user: Observable<firebase.User>;
- private authState: any;
- constructor(private dbAuth: AngularFireAuth,
- private db: AngularFireDatabase,
- private router: Router) {
- this.user = dbAuth.authState;
- }
- authUser() {
- return this.user;
- }
- ....
- }
Add Comment
Please, Sign In to add comment