angular2-guards

Cannot pass data with service in angular2

十年热恋 提交于 2020-06-28 04:16:06
问题 PS: When I talk to my colleague, they told me to get Role's right with once request, if it is not authenticated then reject, else return the data to frond-end. But, I got stuck in use Angular2's Guard. Apps do: Access my routes, and Guard prevent it, the Guard send a request to server to check its auth. Request the server , when server return statue:true and data:[somedatas] then set data with module's dataService , and resolve true for canActivate. Init the target component, in constructor ,

Angular4 - use components service in guard

不羁岁月 提交于 2019-12-24 09:57:08
问题 I have a lazy-loaded-module with a root and child componets. I also have a service to share data between the root and the child components and siblings. The use case behind is a wizard, where the user can set some data in multiple steps. The data of each step is stored globally in the service, so that the parent and every child component is able to access the whole data. Now i want to create guards for the single steps, which should check if the step before was done (data in the service is

Trying to understand the differences between CanActivate and CanActivateChild

亡梦爱人 提交于 2019-12-23 07:24:51
问题 So, I'm trying to protect the access to several routes by using guards. I'm using the following routes to do so : const adminRoutes : Routes = [ { path: 'admin', component: AdminComponent, canActivate: [ AuthGuardService ], children : [ { path: '', canActivateChild: [ AuthGuardService ], children: [ { path: 'edit', component: DashboardComponent}, { path: '', component: DashboardComponent} ] } ] } ]; Here's a look at what AuthGuardService looks like import { Injectable } from '@angular/core';

Trying to understand the differences between CanActivate and CanActivateChild

拟墨画扇 提交于 2019-12-23 07:24:09
问题 So, I'm trying to protect the access to several routes by using guards. I'm using the following routes to do so : const adminRoutes : Routes = [ { path: 'admin', component: AdminComponent, canActivate: [ AuthGuardService ], children : [ { path: '', canActivateChild: [ AuthGuardService ], children: [ { path: 'edit', component: DashboardComponent}, { path: '', component: DashboardComponent} ] } ] } ]; Here's a look at what AuthGuardService looks like import { Injectable } from '@angular/core';

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

半世苍凉 提交于 2019-12-17 16:20:56
问题 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

How to get target route in ng2 guard canActivate?

送分小仙女□ 提交于 2019-12-11 08:02:49
问题 How can I get the route which the user is trying to open within an canActivate guard in angular 2? How can I make the guard watch for every route parameter change eg. a change of the id within the route? import { Injectable } from '@angular/core'; import {Observable, Observer, Subject} from "rxjs/Rx"; @Injectable() export class RightsGuard implements CanActivate { constructor( private router: Router, private route: ActivatedRoute // private state: RouterStateSnapshot ) { } canActivate():

How to make Angular2 Service singleton?

心已入冬 提交于 2019-12-10 12:59:17
问题 I'm trying to implement an auth guard in my app. ie; Only authenticated users can access certain routes of my app. I'm following the tut given here. Once the user is logged in I change a boolean value in my AuthService to true to indicate that the use has logged in. Which needs to be retained through out the life of app. Given below the source code: auth-guard.service.ts import { Injectable } from '@angular/core'; import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot }

Angular 2 roles and permissions

筅森魡賤 提交于 2019-12-09 18:38:43
问题 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:

How to unit-test canActivate guard method of angular2 using Jasmine?

◇◆丶佛笑我妖孽 提交于 2019-12-09 04:37:01
问题 Sorry for asking this type of question. But I'm not able to find any blog or youtube tutorials on writing the canActivate guard file testing. Nor in the official documentation there is anything mentioned. any help will be much appreciated. 回答1: since no one answered my question, so I'm pasting my code snippet for the reference to help people who might get this situation. sampleLoggedIn.guard.ts import {Injectable} from '@angular/core'; import {Router, CanActivate} from '@angular/router';

angular2 guard not working on page refresh

时间秒杀一切 提交于 2019-12-05 02:00:10
问题 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] }