You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

22 lines
598 B

import { inject } from '@angular/core';
import { Router, CanActivateFn } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { map } from 'rxjs/operators';
export const AuthGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const authService = inject(AuthService);
return authService.isAuthenticated$.pipe(
map(isAuthenticated => {
if (isAuthenticated) {
return true;
}
router.navigate(['/auth/login'], {
queryParams: { returnUrl: state.url }
});
return false;
})
);
};