Skip to content

Commit 8c2c13a

Browse files
committed
feat: if authorized, redirect to the article list
1 parent e740d22 commit 8c2c13a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/app/app.routes.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import { authGuard } from './guards/auth-guard'
44
import { AuthPage } from './pages/auth-page/auth-page.component'
55
import { StatusPage } from './pages/status-page/status-page'
66
import { WelcomePage } from './pages/welcome-page/welcome-page'
7+
import { publicGuard } from './guards/public-guard'
78

89
export const routes: Routes = [
9-
{ path: '', component: WelcomePage, pathMatch: 'full' },
10-
{ path: 'auth', component: AuthPage, data: { title: 'Authentication' } },
10+
{ path: '', component: WelcomePage, pathMatch: 'full', canActivate: [publicGuard] },
11+
{
12+
path: 'auth',
13+
component: AuthPage,
14+
data: { title: 'Authentication' },
15+
canActivate: [publicGuard],
16+
},
1117
{
1218
path: 'articles',
1319
loadComponent: async () => {

src/app/guards/public-guard.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CanActivateFn, Router } from '@angular/router'
2+
import { inject } from '@angular/core'
3+
4+
export const publicGuard: CanActivateFn = (route, state) => {
5+
const savedUserId = localStorage.getItem('user')
6+
const path = route.routeConfig?.path
7+
const router = inject(Router)
8+
9+
if (savedUserId && (path === '' || path === 'auth')) {
10+
return router.createUrlTree(['/articles'])
11+
}
12+
13+
return true
14+
}

0 commit comments

Comments
 (0)