### File

`src/app/auth.guard.ts`

### Index

###### Methods

- canActivate
- isLogin

### Constructor

`constructor(dataService: ApiService, router: Router)`
Defined in src/app/auth.guard.ts:14

Parameters :

Name | Type | Optional
dataService | `ApiService` | No
router | `Router` | No

### Methods

canActivate
`canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)`
Defined in src/app/auth.guard.ts:21

Parameters :

Name | Type | Optional
route | `ActivatedRouteSnapshot` | No
state | `RouterStateSnapshot` | No

Returns : `boolean`

isLogin
`isLogin(routeUrl: string)`
Defined in src/app/auth.guard.ts:33

Parameters :

Name | Type | Optional
routeUrl | `string` | No

Returns : `boolean`

    import {Injectable} from '@angular/core';
    import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
    import {ApiService} from './api.service';

    @Injectable({
      providedIn: 'root'
    })

    export class AuthGuard implements CanActivate {
      constructor(private dataService: ApiService, private router: Router) {
      }

      /*! \brief
      This function checks the state of router and calls the boolean function isLogin() to check whether a user is logged in or not.
      */
      canActivate(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
      ): boolean {
        const routeUrl: string = state.url;
        return this.isLogin(routeUrl);
      }

      /*! \brief
      This function calls Api Service to check the login status of the user and redirects the user to appropriate routes according to status.
      */

      isLogin(routeUrl: string): boolean {
        if (this.dataService.isLoggedIn()) {
          return true;
        }
        this.dataService.redirectUrl = routeUrl;
        this.router.navigate(['/login'], {queryParams: {returnUrl: routeUrl}});
      }
    }

# result-matching ""

# No results matching ""