Skip to content

Commit

Permalink
Merge pull request #15 from bento-platform/feat/auto-authenticate
Browse files Browse the repository at this point in the history
feat: add auto-authenticate hook
  • Loading branch information
davidlougheed authored Feb 6, 2024
2 parents a80c5a4 + 6639509 commit 56b4c4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/autoAuthenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from "react";
import { useIsAuthenticated, useOpenIdConfig } from "./hooks";
import { LS_BENTO_WAS_SIGNED_IN, setLSNotSignedIn, usePerformAuth } from "./performAuth";

export interface AutoAuthenticateState {
isAutoAuthenticating: boolean;
}

export const useAutoAuthenticate = (): AutoAuthenticateState => {
const [isAutoAuthenticating, setIsAutoAuthenticating] = useState(false);

const isAuthenticated = useIsAuthenticated();
const openIdConfig = useOpenIdConfig();
const performAuth = usePerformAuth();

const authzEndpoint = openIdConfig?.["authorization_endpoint"];

useEffect(() => {
if (
!isAuthenticated &&
!isAutoAuthenticating &&
authzEndpoint &&
localStorage.getItem(LS_BENTO_WAS_SIGNED_IN) === "true"
) {
console.debug("auto-authenticating");
setLSNotSignedIn();
setIsAutoAuthenticating(true);
performAuth().catch(console.error);
}
}, [authzEndpoint, isAuthenticated, isAutoAuthenticating]);

return { isAutoAuthenticating };
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./autoAuthenticate";
export * from "./contexts";
export * from "./hooks";
export * from "./performAuth";
Expand Down

0 comments on commit 56b4c4a

Please sign in to comment.