Created
July 21, 2018 19:46
-
-
Save ozgurrgul/3a1e39f1ceb0022057469c202b7f1268 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {CanActivate, Router} from '@angular/router'; | |
import {Injectable} from '@angular/core'; | |
import {CustomerService} from './customer.service'; | |
import {ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router/src/router_state'; | |
@Injectable() | |
export class NeedAuthGuard implements CanActivate { | |
constructor(private customerService: CustomerService, private router: Router) { | |
} | |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
const redirectUrl = route['_routerState']['url']; | |
if (this.customerService.isLogged()) { | |
return true; | |
} | |
this.router.navigateByUrl( | |
this.router.createUrlTree( | |
['/login'], { | |
queryParams: { | |
redirectUrl | |
} | |
} | |
) | |
); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment