Skip to content

Instantly share code, notes, and snippets.

@ozgurrgul
Created July 21, 2018 19:46
Show Gist options
  • Save ozgurrgul/3a1e39f1ceb0022057469c202b7f1268 to your computer and use it in GitHub Desktop.
Save ozgurrgul/3a1e39f1ceb0022057469c202b7f1268 to your computer and use it in GitHub Desktop.
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