Read cookie in CanActivate route guard in Angular

Daniel Stephens

I am using Angular 9 and the CanAngular interface to protect a route being called when the user is not logged in. During login I set a cookie session_key, but I don't know how I can read/get the cookie value again in the canActivate function of the interface.

export class AuthGuard implements CanActivate {
    constructor(
        private router: Router,
        private accountService: AccountService
    ) {}

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
            if (cookieExists("session_key")) {  <---------------------- 
                // authorised so return true
                return true;
            }

            // not logged in so redirect to login page with the return url
            this.router.navigate(['/account/login'], { queryParams: { returnUrl: state.url }});
            return false;
    }
}

What existing function would I use for cookieExists?

saravana kumar

If you are using ngx-cookie-service you need to import the Cookie Service in authguard.

import { CookieService } from 'ngx-cookie-service';

and in constructor inject the cookie service

constructor(private cookieService : CookieService){}

Then you can use the below code to access the cookie storage

if (cookieService.get("session_key")) { 
   return true;
}

You can set the cookie using below code

cookieService.set('session-key','some-value');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular Route Guard CanActivate stops routing

How to redirect from parent route to child route in Angular canActivate guard?

Angular 2 Route Guard CanActivate based on Firebase User Role

Get target route URL in CanActivate guard Lazy load modules Angular

Angular Router route guard CanActivate always returns false

Angular canActivate Guard with API

Angular CanActivate Guard

Angular5 canactivate guard :Types of property 'canActivate' are incompatible

Angular CanActivate guard - createUrlTree relative navigation

Angular2 CanActivate guard not working

Angular(5) - lazy loading and canActivate guard - StaticInjectorError

Using defaultIfEmpty() still not emitting in Angular canActivate guard

Angular 2 router canActivate Auth Guard

Authorization Issue with Angular Auth Guard canActivate method

Why does route guard canLoad not fire, but canActivate does

Can't get route params for a lazy module within a CanActivate guard

Angular 2 Route Guard / Auth Guard Security

Angular: Run canActivate on each route change

How to check asynchronously if user is authenticated in router canActivate guard in angular 2?

Return condition always is false into subscribe Angular CanActivate Guard

Return observable inside observable in canactivate guard of Angular 4

Angular guard canActivate method dont work with Observable<boolean>

Angular How to implement CanActivate guard with HTTP Get call

Angular 9 - Guard: canActivate wait until specific condition is met

error in canActivate guard method

Auth Guard in Angular without using LocalStorage or Cookie

Web Api Call from an Angular Route Guard

Implement Angular guard on password reset route

Angular Route Guard based on collection query result