Skip to content

Commit

Permalink
Fix local authorization (#164)
Browse files Browse the repository at this point in the history
* Fix local authorization

* Fix authentication

* Moe signIn and SignOut to its original place

* Not set state when component unmounts

* Remove component unmount changes
  • Loading branch information
JonasDov authored Oct 28, 2024
1 parent 3f71add commit 9f5c2f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"monaco-editor": "^0.40.0",
"monaco-editor-webpack-plugin": "^7.1.0",
"npm-run-all": "^4.1.5",
"oidc-client-ts": "^3.0.1",
"oidc-client-ts": "^3.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-error-boundary": "^4.0.13",
Expand Down
68 changes: 31 additions & 37 deletions app/frontend/src/app/Authorization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,24 @@ export function createAuthorizationProvider(config: AuthorizationProviderConfig)
const demoAuthorizationClient = new DemoAuthClient();

const signIn = async () => {
await userManager.signinRedirect({
state: window.location.pathname + window.location.search + window.location.hash,
});
try {
await userManager.signinRedirect({
state: window.location.pathname + window.location.search + window.location.hash,
});
} catch (err) {
// eslint-disable-next-line no-console
console.warn(err);
}
};

const signOut = async () => {
try {
await userManager.signoutRedirect();
} catch (err) {
// eslint-disable-next-line no-console
console.warn(err);
}
};
const signOut = async () => userManager.signoutRedirect();

return function AuthorizationProvider(props: React.PropsWithChildren<{}>): React.ReactElement {
const [authorizationContextValue, setAuthorizationContextValue] = React.useState<AuthorizationContext>({
Expand Down Expand Up @@ -98,43 +111,24 @@ export function createAuthorizationProvider(config: AuthorizationProviderConfig)
userManager.events.addUserLoaded(handleUserLoaded);
userManager.events.addUserUnloaded(handleUserUnloaded);

return () => {
userManager.events.removeUserLoaded(handleUserLoaded);
userManager.events.removeUserUnloaded(handleUserUnloaded);
};
}, []);

React.useEffect(() => {
if (window.self !== window.top) {
// It could be that parent document has already initiated silent sign-in in an invisible iframe, and now the
// identity provider has redirected the iframe back to the app.
return;
}

let disposed = false;
void (async () => {
try {
await userManager.signinSilent();
await userManager.clearStaleState();
} catch (error) {
if (disposed) {
userManager
.getUser()
.then((user) => {
if (user === null) {
handleUserUnloaded();
return;
}

setAuthorizationContextValue({
userManager,
demoAuthorizationClient,
state: AuthorizationState.SignedOut,
user: undefined,
userAuthorizationClient: undefined,
signIn,
signOut,
});
}
})();
handleUserLoaded(user);
})
.catch((e) => {
// eslint-disable-next-line no-console
console.warn(e);
handleUserUnloaded();
});

return () => {
disposed = true;
userManager.events.removeUserLoaded(handleUserLoaded);
userManager.events.removeUserUnloaded(handleUserUnloaded);
};
}, []);

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f5c2f1

Please sign in to comment.