angular2-guards

Angular 2 roles and permissions

我们两清 提交于 2019-12-04 09:33:47
I have used angular2 and laravel 5.3 in my project. in laravel when user logged in server will be send the permissions of the user to handle authorization in angular. so I wrote a guard to protect routes from users that cannot access. here is my guard class code: export class AccessGuard implements CanActivate{ permissions; currentRoute; constructor(private authService:AuthService,private router:Router){ this.permissions = this.authService.getPermissions(); } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){ return this.checkHavePermission(state.url); } canActivateChild

angular2: CanDeactivate guard

怎甘沉沦 提交于 2019-12-04 08:26:36
问题 I've created a CanDeactivate guard which returns an observable and it's applied to a component which is loaded in a inner nested router-outlet. Should this guard be called whenever one tries to navigate to another url? I'm asking this because this is not happening in my case. In my case, the guard will only get called for the first "different" URL. Let me try to explain it with an example. Assume I'm always returning false and I'm trying to navigate to different urls from the same component:

angular2 guard not working on page refresh

为君一笑 提交于 2019-12-03 16:27:56
Before every request I want to be sure that there is a user profile available. I use a canActivateChild guard to do this. According to the documentation of angular2 it is possible to return an observable: https://angular.io/api/router/CanActivateChild app.routes.ts export const routes: Routes = [ { path: '', canActivateChild: [ProfileGuard], children: [ { path: 'home', component: HomeComponent, canActivate: [AuthGuard] }, { path: 'login', component: LoginComponent, canActivate: [GuestGuard] }, } ]; The canActivatedChild is executed before the child route canActivate profile.guard.ts: export

Using BehaviorSubject in auth-guard's canActivate

孤街醉人 提交于 2019-12-03 15:06:13
问题 What I want to achieve: I want to share authentication state across my application using BehaviorSubject. I use the authentication state e.g. inside an auth-guard to prevent the user from visiting login/register pages when the user already is authenticated. Problem: because the BehaviorSubject has a initial value, which is false (not logged in), it seems that the auth-guard takes this first value, instead of waiting for the uid-sync. AuthInfo (Auth state store): export class AuthInfo {

Using BehaviorSubject in auth-guard's canActivate

你说的曾经没有我的故事 提交于 2019-12-03 03:59:01
What I want to achieve: I want to share authentication state across my application using BehaviorSubject. I use the authentication state e.g. inside an auth-guard to prevent the user from visiting login/register pages when the user already is authenticated. Problem: because the BehaviorSubject has a initial value, which is false (not logged in), it seems that the auth-guard takes this first value, instead of waiting for the uid-sync. AuthInfo (Auth state store): export class AuthInfo { constructor(public uid: string) {} isLoggedIn() { return !!this.uid; } } AuthService : @Injectable() export

angular2: CanDeactivate guard

跟風遠走 提交于 2019-12-02 23:06:29
I've created a CanDeactivate guard which returns an observable and it's applied to a component which is loaded in a inner nested router-outlet. Should this guard be called whenever one tries to navigate to another url? I'm asking this because this is not happening in my case. In my case, the guard will only get called for the first "different" URL. Let me try to explain it with an example. Assume I'm always returning false and I'm trying to navigate to different urls from the same component: /A --> guard called /B --> guard called /B --> no navigation and no guard called /A --> guard called /A

Angular 2 get current route in guard

雨燕双飞 提交于 2019-11-30 04:39:42
I have an AccessGuard class in my project which its work is to determine if user can access to the route or not. I used the router.url to get the current route but the url returns the route before navigation to the new route like I'm in the users route and I click on the candidates route so the url returns users instead of candidates which I want that to validate access to the route this is my route file: const routes:Routes = [ { path:'', component:PanelComponent, canActivate:[AuthGuard,AccessGuard], canActivateChild:[AuthGuard,AccessGuard], children:[ { path:'dashboard', component

Angular2 route guard returning Observable<bool>, how to handle errors

为君一笑 提交于 2019-11-30 03:50:18
问题 I have a route guard like below @Injectable() export class AuthGuard implements CanActivate { constructor(private router: Router, private authenticationSvc: AuthenticationService) { } canActivate(): Observable<boolean> { return this.authenticationSvc.getAuthenticatedUser().map( r => { if (this.authenticationSvc.isAuthenticated()) { // logged in so return true return true; } this.router.navigateByUrl('/login'); return false; }) } The issue is that sometimes getAuthenticatedUser returns a 401,

Angular2: Global Guard (user has to be logged in always)

孤街醉人 提交于 2019-11-27 22:25:52
I'm building an application where there's no access at all for unauthenticated users. I wrote a LoggedInGuard , but now I have to add canActivate: [LoggedInGuard] to every route inside my router configuration (except the LoginComponent ). Is there a better way to get this working? My file / module layout looks like this: app/ AppModule AppRoutingModule AppComponent authentication/ AuthenticationModule AuthenticationRoutingModule LoginComponent contacts/ ContactsModule ContactsRoutingModule ContactListComponent users/ UsersModule UsersRoutingModule UserEditComponent ... Maybe it's possible to