update routing client
This commit is contained in:
parent
d7b7bc7be0
commit
218b2de59a
18 changed files with 377 additions and 148 deletions
|
|
@ -1,11 +1,62 @@
|
|||
import { NgModule } from '@angular/core';
|
||||
import { RouterModule, Routes } from '@angular/router';
|
||||
import { NgModule, inject } from '@angular/core';
|
||||
import { CanActivateFn, Router, RouterModule, Routes } from '@angular/router';
|
||||
import { UserService } from './core/services/user.service';
|
||||
import { map } from 'rxjs';
|
||||
|
||||
const authGuard: CanActivateFn = () => {
|
||||
const userService: UserService = inject(UserService);
|
||||
const router: Router = inject(Router);
|
||||
return userService.isAuthenticated.pipe(
|
||||
map((isAuth) => isAuth || router.parseUrl('/login'))
|
||||
);
|
||||
};
|
||||
|
||||
const loginGuard: CanActivateFn = () => {
|
||||
const userService: UserService = inject(UserService);
|
||||
const router: Router = inject(Router);
|
||||
|
||||
return userService.isAuthenticated.pipe(
|
||||
map((isAuth) => {
|
||||
if (!isAuth) {
|
||||
return true;
|
||||
}
|
||||
return router.parseUrl('/dashboard');
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const routes: Routes = [
|
||||
{
|
||||
path: 'login',
|
||||
loadComponent: () =>
|
||||
import('./core/auth/auth.component').then((m) => m.AuthComponent),
|
||||
canActivate: [loginGuard],
|
||||
},
|
||||
{
|
||||
path: 'register',
|
||||
loadComponent: () =>
|
||||
import('./core/auth/auth.component').then((m) => m.AuthComponent),
|
||||
canActivate: [loginGuard],
|
||||
},
|
||||
{
|
||||
path: '',
|
||||
loadComponent: () =>
|
||||
import('./features/home/home.component').then((m) => m.HomeComponent),
|
||||
import('./core/layout/layout.component').then((m) => m.LayoutComponent),
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
pathMatch: 'full',
|
||||
redirectTo: 'dashboard',
|
||||
},
|
||||
{
|
||||
path: 'dashboard',
|
||||
loadComponent: () =>
|
||||
import('./features/dashboard/dashboard.component').then(
|
||||
(m) => m.DashboardComponent
|
||||
),
|
||||
canActivate: [authGuard],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue