Tibo
Tibo

Reputation: 49

Angular2 authguard routes

Im developing an application web and now im having troubles with routes. I created a AuthGuard that return if there is a user logged in or not. Well, the problem starts when i want to split in two types of routes:

-Routes where i only can go if im not logged in. -Routes where i only can go if im logged in.

So if im logged in what i want is that i cant go to some routes.

This is my router:

{ path: 'app',
component: AppComponent,
children: [
  { path: 'login', component: LoginComponent },
  { path: 'inicio', component: ContentHomeComponent },
  { path: 'register', component: RegisterFormComponent},
  { path: 'apuestas', component: ContentBetComponent, canActivate: [AuthGuard]}
]},
  { path: '**', redirectTo:'/app/inicio',pathMatch: 'full' }

What i have now is that i only can go to "apuestas" if i logged in but i want to separate in two how i said up. I want that login, inicio and register can be only accessed if you are not logged in, and the others if you are registered.

Upvotes: 1

Views: 152

Answers (1)

Naduweisstschon
Naduweisstschon

Reputation: 88

If I understand correctly you can just add another NotAuthenticatedGuard that returns false on canActivate if the user is logged in.

{ path: 'app',
  component: AppComponent,
  children: [
    { path: 'login', component: LoginComponent, canActivate: [ NotAuthGuard ] },
    { path: 'inicio', component: ContentHomeComponent, canActivate: [ NotAuthGuard ] },
    { path: 'register', component: RegisterFormComponent, canActivate: [ NotAuthGuard ] },
    { path: 'apuestas', component: ContentBetComponent, canActivate: [AuthGuard]}
]},
{ path: '**', redirectTo:'/app/inicio',pathMatch: 'full' }

The naming is up to you of course.

Upvotes: 1

Related Questions