Skip to content

Commit

Permalink
fix(auth): fix endless authentication loop
Browse files Browse the repository at this point in the history
Redirect to auth page was not working correctly; leading to endless
redirects causing browser to crash.
  • Loading branch information
Gijsdeman committed Nov 8, 2024
1 parent 03d78bd commit 69f4f44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ router.beforeEach(async (to, from, next) => {
layoutStore.init();

const authStore = useAuthStore();
// Automatically login using mock when in devops mode
// Automatically login using mock when in development mode
if (!import.meta.env.PROD && !authStore.isAuthenticated()) {
await authStore.MockLogin({
id: 'dev',
Expand All @@ -156,7 +156,7 @@ router.beforeEach(async (to, from, next) => {
hasRights = authStore.isInSecurityGroup(to.meta.securityGroup, to.meta.securitySection);
}

if (!authenticated) {
if (!authenticated && !to.path.startsWith('/auth')) {
// If not authenticated; redirect to login
next({ name: 'auth', query: { path: to.fullPath } });
} else if (authenticated && !hasRights) {
Expand Down
11 changes: 7 additions & 4 deletions src/stores/auth.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export const useAuthStore = defineStore('auth', {
*/
async init(): Promise<void> {
getInformation().then(async (user) => {
this.name = user.data!.name;
this.roles = user.data!.roles;
if (user.data) {
this.name = user.data!.name;
this.roles = user.data!.roles;

await useHandlersStore().init();
await useColorStore().init();
await useHandlersStore().init();
await useColorStore().init();
}
});

const securityGroups = await getSecurityGroups();
Expand Down Expand Up @@ -124,6 +126,7 @@ export const useAuthStore = defineStore('auth', {
* @param section - The security group to check for
*/
isInSecurityGroup(group: keyof ISecurityGroups, section: keyof ISecuritySections): boolean {
if (!this.securityGroups) return false;
return _.intersection(this.roles, this.securityGroups![group][section]).length > 0;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/views/Base/AuthView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ onMounted(async () => {
session_state: queryParameters.get('session_state')!
})
.then(() => {
router.push(url!);
authStore.init();
router.push(url ?? '/');
})
.catch(() => {
router.push({ name: 'notFound' });
Expand All @@ -71,6 +72,9 @@ onMounted(async () => {
sessionStorage.setItem('url', route.query.path as string);
const oidcParameters = await getOidcParameters();
// If the oidc parameters are not available, retry auth
if (!oidcParameters.data) await router.push({ name: 'auth' });
let queryParameters = new URLSearchParams({
client_id: oidcParameters.data!.clientId,
redirect_uri: oidcParameters.data!.redirectUri,
Expand Down

0 comments on commit 69f4f44

Please sign in to comment.