Skip to content

Commit

Permalink
fix: prevent race condition in unauthorized handler
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Apr 20, 2023
1 parent b7994b5 commit dd4e63c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions addon/unauthorized.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getOwner } from "@ember/application";
import { next } from "@ember/runloop";
import { later } from "@ember/runloop";
import { isTesting, macroCondition } from "@embroider/macros";

import { getConfig } from "ember-simple-auth-oidc/config";
Expand All @@ -15,13 +15,13 @@ export default function handleUnauthorized(session) {
if (macroCondition(isTesting())) {
// don't redirect in tests
} else {
// defer the redirect, so the first unauthenticated request can trigger
// the redirect to the login page before another request will intercept
// the process and a race condition would start.
next(() => {
// Defer the redirect, so we can collect all unauthorized requests and trigger a final
// redirect. We don't want to interrupt calls to the authorization endpoint nor create race
// conditions when multiple requests land in this unauthorized handler.
later(() => {
location.replace(
getAbsoluteUrl(getConfig(getOwner(session)).afterLogoutUri || "")
);
});
}, 1000);
}
}

0 comments on commit dd4e63c

Please sign in to comment.