File tree Expand file tree Collapse file tree 1 file changed +9
-1
lines changed
Expand file tree Collapse file tree 1 file changed +9
-1
lines changed Original file line number Diff line number Diff line change 11import { CanMatchFn , Router } from '@angular/router'
2- import { inject } from '@angular/core'
2+ import { DestroyRef , inject } from '@angular/core'
33import { AuthService } from '../services/auth-service'
44import { map } from 'rxjs/operators'
55import { UserService } from '../services/user-service'
6+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'
7+ import { catchError , of } from 'rxjs'
8+ import { HttpErrorResponse } from '@angular/common/http'
69
710export const authGuard : CanMatchFn = ( route , segments ) => {
811 const savedUserId = localStorage . getItem ( 'user' )
912
1013 const router = inject ( Router )
1114 const authService = inject ( AuthService )
1215 const userService = inject ( UserService )
16+ const destroyRef = inject ( DestroyRef )
1317
1418 if ( savedUserId ) {
1519 return userService . getUser ( ) . pipe (
20+ takeUntilDestroyed ( destroyRef ) ,
21+ catchError ( ( error : HttpErrorResponse ) => {
22+ return of ( null )
23+ } ) ,
1624 map ( ( user ) => {
1725 if ( user ) {
1826 authService . updateAuth ( user )
You can’t perform that action at this time.
0 commit comments