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

178 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-12-12 00:23:20 +07:00
import { inject, NgModule } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivateFn,
Router,
RouterModule,
RouterStateSnapshot,
Routes,
} from '@angular/router';
2023-12-12 00:23:20 +07:00
import { UserService } from './core/services/user.service';
import { map } from 'rxjs';
import { UserPermissions } from './core/auth/userPermissions';
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
);
};
2023-12-12 00:23:20 +07:00
const permissionsGuard: (
...requiredPermissions: UserPermissions[]
) => CanActivateFn =
(...requiredPermissions) =>
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
const userService: UserService = inject(UserService);
const router: Router = inject(Router);
const user = userService.getCurrentUser();
if (user == null)
return router.createUrlTree(['/login'], {
queryParams: {
redirectUrl: state.url,
},
});
2023-09-18 08:50:13 +07:00
2023-12-12 00:23:20 +07:00
if (
requiredPermissions.every((permission) =>
user.permissions.includes(permission)
)
) {
return true;
}
return router.createUrlTree(['/unauthorized'], {
2023-12-08 14:46:07 +07:00
queryParams: {
redirectUrl: state.url,
},
});
2023-12-12 00:23:20 +07:00
};
2023-12-08 14:46:07 +07:00
2023-12-12 00:23:20 +07:00
const loginGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => {
2023-12-08 14:46:07 +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-12-12 00:23:20 +07:00
router.parseUrl(route.queryParams['redirectUrl'] ?? 'departments'),
]);
// 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
{
2023-12-12 00:23:20 +07:00
path: 'departments',
loadComponent: () =>
import('./core/department/department.component').then(
(m) => m.DepartmentComponent
),
canActivate: [authGuard],
},
{
path: ':department',
2023-09-14 14:52:04 +07:00
loadComponent: () =>
2023-09-18 08:50:13 +07:00
import('./core/layout/layout.component').then((m) => m.LayoutComponent),
children: [
{
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-10-27 16:13:04 +07:00
(m) => m.RecipesComponent
2023-09-18 08:50:13 +07:00
),
2023-12-12 00:23:20 +07:00
canActivate: [
authGuard,
permissionsGuard(UserPermissions.THAI_PERMISSION),
],
2023-09-18 08:50:13 +07:00
},
{
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'
2023-12-12 00:23:20 +07:00
).then((m) => m.RecipeDetailsComponent),
canActivate: [
authGuard,
permissionsGuard(UserPermissions.THAI_PERMISSION),
],
},
2024-01-24 16:25:49 +07:00
{
path: 'log',
loadComponent: () =>
import('./features/changelog/changelog.component').then(
(m) => m.ChangelogComponent
),
},
{
path: 'materials',
loadComponent: () =>
import('./features/material-settings/material-settings.component').then(
(m) => m.MaterialSettingsComponent
),
}
2023-09-18 08:50:13 +07:00
],
2023-09-14 14:52:04 +07:00
},
{
2023-12-12 00:23:20 +07:00
path: '',
pathMatch: 'full',
redirectTo: 'departments',
},
{
2023-12-12 00:23:20 +07:00
path: 'unauthorized',
loadComponent: () =>
import('./core/auth/unauthorized.component').then(
(m) => m.UnauthorizedComponent
),
},
{
2023-12-12 00:23:20 +07:00
path: 'notfound',
loadComponent: () =>
import('./core/notfound.component').then((m) => m.NotfoundComponent),
},
{
path: '**',
redirectTo: 'notfound',
},
2023-09-14 14:52:04 +07:00
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
2023-12-12 00:23:20 +07:00
export class AppRoutingModule {}