File

src/app/home/user-type.guard.ts

Index

Properties
Methods

Constructor

constructor(sharedPreferences: SharedPreferences, router: Router, appGlobalService: AppGlobalService)
Parameters :
Name Type Optional
sharedPreferences SharedPreferences No
router Router No
appGlobalService AppGlobalService No

Methods

Async canActivate
canActivate()
Returns : Promise<boolean>
import { Inject, Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { PreferenceKey, RouterLinks } from '@app/app/app.constant';
import { AppGlobalService } from '@app/services/app-global-service.service';
import { ProfileType, SharedPreferences } from 'sunbird-sdk';

@Injectable()
export class UserTypeGuard implements CanActivate {
    constructor(
        @Inject('SHARED_PREFERENCES') private sharedPreferences: SharedPreferences,
        private router: Router,
        public appGlobalService: AppGlobalService,
    ) {
    }
    async canActivate(): Promise<boolean> {
        const isAdminUser = (await this.sharedPreferences.getString(PreferenceKey.SELECTED_USER_TYPE).toPromise() === ProfileType.ADMIN);
        if (isAdminUser && this.appGlobalService.isUserLoggedIn()) {
            this.router.navigate([`/${RouterLinks.HOME_TAB}/admin`]);
        } else {
            this.router.navigate([`/${RouterLinks.HOME_TAB}/user`]);
        }
        return false;
    }
}

results matching ""

    No results matching ""