Thursday, 22 November 2018

AuthService in angular

path: 'home',
component: HomeComponent,
canActivate: [AuthService], data: { title: 'Home' },

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot }
from '@angular/router';
import { GlobalService } from './global.service';
import { CommanService } from './comman.service';

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor(private router: Router, protected global: GlobalService,
protected comm: CommanService) { }


canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let user: any = JSON.parse(localStorage.getItem('AccountDetail'));
try {

// this.comm.currentUrl.subscribe(data => {
// console.log("cUrl:", data);
// var currentUrl = data;
// this.comm.isAdmin.subscribe(data2 => {
// var isAdmin = data2;
// if (isAdmin) {
// } else {
// }
// });
// });

console.log("MasterId", user.MasterId);
if (user.MasterId > 0) {
console.log("ReferredBy", user.ReferredBy);
if (user.ReferredBy > 0) {//For User
console.log("ReferredBy:");
var currentUrl = state.url;
if (currentUrl == '/home/allaccounts') {
//not add currentUrl == '/home/subscribe'
return false;
} else {
return true;
}

} else {//For Master
return true;
}

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


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

}

No comments:

Post a Comment

IIS deployment support details

  Node JS - IIS deployment support details node: http://go.microsoft.com/?linkid=9784334 IISNode: https://github.com/azure/iisnode/releases/...