Taobin-Recipe-Manager/client/src/app/app-routing.module.ts

116 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-09-18 08:50:13 +07:00
import { NgModule, inject } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivateFn,
Router,
RouterModule,
RouterStateSnapshot,
Routes,
} from '@angular/router';
2023-09-18 08:50:13 +07:00
import { UserService } from './core/services/user.service';
2023-10-06 09:11:01 +07:00
import { map } from 'rxjs';
2023-09-18 08:50:13 +07:00
const authGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) => {
2023-09-18 08:50:13 +07:00
const userService: UserService = inject(UserService);
const router: Router = inject(Router);
2023-09-18 08:50:13 +07:00
return userService.isAuthenticated.pipe(
map((isAuth) => {
if (isAuth) {
return true;
}
return router.createUrlTree(['/login'], {
queryParams: {
redirectUrl: state.url,
},
});
})
2023-09-18 08:50:13 +07:00
);
};
const loginGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) => {
2023-09-18 08:50:13 +07:00
const userService: UserService = inject(UserService);
const router: Router = inject(Router);
return userService.isAuthenticated.pipe(
map((isAuth) => {
if (!isAuth) {
return true;
2023-09-18 08:50:13 +07:00
}
// redirect to redirectUrl query param
console.log(route.queryParams['redirectUrl']);
return router.createUrlTree([
2023-10-06 09:11:01 +07:00
router.parseUrl(route.queryParams['redirectUrl'] ?? '/recipes'),
]);
// return false;
2023-09-18 08:50:13 +07:00
})
);
2023-09-18 08:50:13 +07:00
};
2023-09-14 14:52:04 +07:00
const routes: Routes = [
2023-09-18 08:50:13 +07:00
{
path: 'login',
loadComponent: () =>
import('./core/auth/auth.component').then((m) => m.AuthComponent),
canActivate: [loginGuard],
},
{
path: 'callback',
2023-09-18 08:50:13 +07:00
loadComponent: () =>
import('./core/callback/callback.component').then(
(m) => m.CallbackComponent
),
2023-09-18 08:50:13 +07:00
},
2023-09-14 14:52:04 +07:00
{
path: '',
loadComponent: () =>
2023-09-18 08:50:13 +07:00
import('./core/layout/layout.component').then((m) => m.LayoutComponent),
children: [
{
path: '',
pathMatch: 'full',
2023-10-06 09:11:01 +07:00
redirectTo: 'recipes',
2023-09-18 08:50:13 +07:00
},
{
2023-10-06 09:11:01 +07:00
path: 'recipes',
2023-09-18 08:50:13 +07:00
loadComponent: () =>
2023-10-06 09:11:01 +07:00
import('./features/recipes/recipes.component').then(
2023-09-18 08:50:13 +07:00
(m) => m.DashboardComponent
),
canActivate: [authGuard],
},
{
2023-10-05 16:04:08 +07:00
path: 'recipe/:productCode',
loadComponent: () =>
import(
2023-10-06 09:11:01 +07:00
'./features/recipes/recipe-details/recipe-details.component'
).then((m) => m.RecipeDetailsComponent),
canActivate: [authGuard],
},
2023-10-20 14:32:16 +07:00
// {
// path: 'log',
// loadComponent: () =>
// import('./features/changelog/changelog.component').then(
// (m) => m.ChangelogComponent
// ),
// },
{
path: '**',
2023-10-06 09:11:01 +07:00
redirectTo: 'recipes',
},
2023-09-18 08:50:13 +07:00
],
2023-09-14 14:52:04 +07:00
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}