Skip to content

Commit 25bed15

Browse files
committed
feat: add authenticate guard for private routes
1 parent a26b45f commit 25bed15

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { CanActivateFn } from '@angular/router';
3+
4+
import { authenticatedGuard } from './authenticated.guard';
5+
6+
describe('authenticatedGuard', () => {
7+
const executeGuard: CanActivateFn = (...guardParameters) =>
8+
TestBed.runInInjectionContext(() => authenticatedGuard(...guardParameters));
9+
10+
beforeEach(() => {
11+
TestBed.configureTestingModule({});
12+
});
13+
14+
it('should be created', () => {
15+
expect(executeGuard).toBeTruthy();
16+
});
17+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { inject } from '@angular/core';
2+
import { CanActivateFn, Router } from '@angular/router';
3+
import { UserStore } from '../user.store';
4+
5+
export const authenticatedGuard: CanActivateFn = (route, state) => {
6+
const userStore = inject(UserStore);
7+
const router = inject(Router);
8+
9+
if (userStore.currentUser) return true;
10+
router.navigate(['/']).then();
11+
return false;
12+
};

0 commit comments

Comments
 (0)