I am building a small project and I am basically dealing with Adding an Angular route guard. I ran into the error 'Not all code paths return a value.' What could be wrong? I am new to development.
auth.guard.ts
import { map } from 'rxjs/operators';
import { AccountService } from './../_services/account.service';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from
'@angular/router';
import { Observable } from 'rxjs';
import { ToastrService } from 'ngx-toastr';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private accountService: AccountService, private toastr:
ToastrService) { }
canActivate(): Observable<boolean> {
return this.accountService.currentUser$.pipe(
map(user => {
if (user) {
return true;
}
this.toastr.error('Access Denied');
}
)
)
}
}
app-routing.module.ts
import { MemberDetailComponent } from './members/member-detail/member-detail.component';
import { MemberListComponent } from './members/member-list/member-list.component';
import { HomeComponent } from './home/home.component';
import { NgModule, Component } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ListsComponent } from './lists/lists.component';
import { MessagesComponent } from './messages/messages.component';
import { AuthGuard } from './_guards/auth.guard';
const routes: Routes = [
{path: '', component: HomeComponent},
{path: 'members', component: MemberListComponent, canActivate: [AuthGuard]},
{path: 'members/:id', component: MemberDetailComponent} ,
{path: 'lists', component: ListsComponent} ,
{path: 'messages', component: MessagesComponent} ,
{path: '**', component: HomeComponent, pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Here is a screenshot of the auth.guard.ts
The app-routing.module.ts file