When I add AuthGuard service with canActivate
on routes, the app crashes when I try to go to /profile
and it redirect me to localhost:4200
, not even /home
and gives this error:
ERROR Error: "[object Object]"
My code :
app.routing.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, CanActivate } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProfileComponent } from './profile/profile.component';
import { LoginComponent } from './login/login.component';
import { AuthGuardService as AuthGuard } from './auth-guard.service';
const routes: Routes = [
{path:'', redirectTo:'/home', pathMatch:'full'},
{path:'home',component: HomeComponent},
{path:'login',component: LoginComponent},
{path:'profile',component: ProfileComponent,canActivate: [AuthGuard]}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [AuthGuard],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { ProfileComponent } from './profile/profile.component';
import { TopbarComponent } from './topbar/topbar.component';
import { AuthGuardService as AuthGuard} from './auth-guard.service';
import { AuthService} from './auth.service';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent,
ProfileComponent,
TopbarComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [AuthGuard, AuthService],
bootstrap: [AppComponent]
})
export class AppModule { }
auth-guard.servce.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(public auth: AuthService, public router: Router) { }
canActivate():boolean{
if (localStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page
this.router.navigate(['/login']);
return false;
}
}
It doesn't work!
CanActivatelinkInterface that a class can implement to be a guard deciding if a route can be activated. If all guards return true , navigation continues. If any guard returns false , navigation is cancelled.
The canActivate guard checks if the user can visit the specific route or we have to prevent access to that specific route. We use the this guard when we have to check some condition and based on that users have the access to reach that specific route or not, before activating the component or showing it to the user.
CanActivateChild - Decides if children routes of a route can be activated. CanDeactivate - Decides if a route can be deactivated.
The differences If we directly navigate to the child route, the canActivate guard will also be executed. canActivateChild will always be executed while navigating to/between child routes. For example, if we're at a child route child/1 and we navigate to child/2 , the guard will get executed.
Had this same issue, solution was to add the Auth Guard to my providers in my app.module.ts
You should also make sure that this is the only file in which it is added to the providers.
import { AuthGuardService } from './auth-guard.service';
@NgModule({
declarations: [
// whatever your declarations are
],
imports: [
// Whatever your imports are
],
providers: [
AuthGuardService
],
bootstrap: [AppComponent]
})
export class AppModule { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With