feat: First-Start Create Admin Modal 1.01
This commit is contained in:
@@ -2,22 +2,36 @@ import { inject } from '@angular/core';
|
||||
import { CanActivateFn, Router } from '@angular/router';
|
||||
import { AuthService } from '../services/auth.service';
|
||||
import { BootstrapService } from '../services/bootstrap.service';
|
||||
import { decideAdminGuard } from './admin.guard.decision';
|
||||
import { decideAdminGuard, AdminGuardDecision } from './admin.guard.decision';
|
||||
|
||||
export { decideAdminGuard } from './admin.guard.decision';
|
||||
export type { AdminGuardDecision } from './admin.guard.decision';
|
||||
|
||||
export interface AdminGuardDeps {
|
||||
auth: AuthService;
|
||||
bootstrap: BootstrapService;
|
||||
router: Router;
|
||||
}
|
||||
|
||||
export async function decideAdminAccessGuard(
|
||||
deps: AdminGuardDeps,
|
||||
): Promise<true | ReturnType<Router['createUrlTree']>> {
|
||||
await deps.bootstrap.ready();
|
||||
await deps.auth.waitUntilHydrated();
|
||||
|
||||
const decision: AdminGuardDecision = decideAdminGuard({
|
||||
initialized: deps.bootstrap.initialized(),
|
||||
isAuthenticated: deps.auth.isAuthenticated(),
|
||||
role: deps.auth.currentUser()?.role,
|
||||
});
|
||||
|
||||
if (decision.kind === 'allow') return true;
|
||||
return deps.router.createUrlTree([decision.path]);
|
||||
}
|
||||
|
||||
export const adminGuard: CanActivateFn = () => {
|
||||
const auth = inject(AuthService);
|
||||
const bootstrap = inject(BootstrapService);
|
||||
const router = inject(Router);
|
||||
|
||||
const decision = decideAdminGuard({
|
||||
initialized: bootstrap.initialized(),
|
||||
isAuthenticated: auth.isAuthenticated(),
|
||||
role: auth.currentUser()?.role,
|
||||
});
|
||||
|
||||
if (decision.kind === 'allow') return true;
|
||||
return router.createUrlTree([decision.path]);
|
||||
};
|
||||
return decideAdminAccessGuard({ auth, bootstrap, router });
|
||||
};
|
||||
Reference in New Issue
Block a user