Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- @Injectable()
- export class AuthService {
- public isAuthenticated = false;
- constructor(private http: HttpClient) {
- }
- setUserLoggedIn() {
- this.isAuthenticated = true;
- }
- getUserLoggedIn() {
- return this.isAuthenticated;
- }
- public setAuthenticated() {
- this.http
- .get<any>(url, {withCredentials: true, observe: 'response'})
- .subscribe(
- (res) => {
- console.log('Status: Authenticated: ' + res.headers.get('status') );
- if ( res.headers.get('status') === '200') {
- this.setUserLoggedIn();
- }
- },
- err => {});
- }
- }
- import { Injectable } from '@angular/core';
- import { CanActivate,
- ActivatedRouteSnapshot,
- RouterStateSnapshot,
- Router,
- ActivatedRoute} from '@angular/router';
- import { Observable } from 'rxjs/Observable';
- import { HttpClient } from '@angular/common/http';
- import { AuthService } from './service/auth.service';
- @Injectable()
- export class AuthguardGuard implements CanActivate {
- constructor(
- private user: AuthService,
- public router: Router,
- private http: HttpClient,
- private activatedRoute: ActivatedRoute) {
- }
- canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
- if ( !this.user.isAuthenticated ) {
- console.log('Not authenticated!');
- this.router.navigate(['userLogin']);
- return false;
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment