23 lines
587 B
TypeScript
23 lines
587 B
TypeScript
import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';
|
|
import { inject } from '@angular/core';
|
|
import { KeycloakService } from 'keycloak-angular';
|
|
|
|
export const authGuard: CanActivateFn = async (
|
|
route: ActivatedRouteSnapshot,
|
|
state: RouterStateSnapshot
|
|
) => {
|
|
const keycloakService = inject(KeycloakService);
|
|
const isLoggedIn = keycloakService.isLoggedIn();
|
|
|
|
if (isLoggedIn) {
|
|
return true;
|
|
}
|
|
|
|
const baseurl = window.location.origin;
|
|
|
|
keycloakService.login({
|
|
redirectUri: `${baseurl}${state.url}`,
|
|
});
|
|
|
|
return false;
|
|
};
|