Skip to content

Commit

Permalink
feat: Add onBeforeSignIn hook
Browse files Browse the repository at this point in the history
Also allow non async functions in onSignIn/Out
  • Loading branch information
hmvp committed Apr 16, 2020
1 parent 9247d78 commit e2f609e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/interfaces/authproviderprops.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [autoSignIn](authproviderprops.md#optional-autosignin)
* [clientId](authproviderprops.md#optional-clientid)
* [location](authproviderprops.md#optional-location)
* [onBeforeSignIn](authproviderprops.md#optional-onbeforesignin)
* [onSignIn](authproviderprops.md#optional-onsignin)
* [onSignOut](authproviderprops.md#optional-onsignout)
* [redirectUri](authproviderprops.md#optional-redirecturi)
Expand Down Expand Up @@ -53,6 +54,16 @@ Defaults to `windows.location`.

___

### `Optional` onBeforeSignIn

**onBeforeSignIn**? : *undefined | function*

On before sign in hook. Can be use to store the current url for use after signing in.

This only gets called if autoSignIn is true

___

### `Optional` onSignIn

**onSignIn**? : *undefined | function*
Expand Down
23 changes: 14 additions & 9 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ export interface AuthProviderProps {
* defaults to true
*/
autoSignIn?: boolean;

/**
* On before sign in hook. Can be use to store the current url for use after signing in.
*
* This only gets called if autoSignIn is true
*/
onBeforeSignIn?: () => void;
/**
* On sign out hook. Can be a async function.
* @param userData User
*/
onSignIn?: (userData: User | null) => Promise<void>;
onSignIn?: (userData: User | null) => Promise<void> | void;
/**
* On sign out hook. Can be a async function.
*/
onSignOut?: () => Promise<void>;
onSignOut?: () => Promise<void> | void;
}

export interface AuthContextProps {
Expand All @@ -74,6 +81,7 @@ export const AuthProvider: FC<AuthProviderProps> = (props) => {
const {
children,
autoSignIn = true,
onBeforeSignIn,
onSignIn,
onSignOut,
location = window.location,
Expand All @@ -82,13 +90,7 @@ export const AuthProvider: FC<AuthProviderProps> = (props) => {
const [userData, setUserData] = useState<User | null>(null);

if (!userManager) {
const {
authority,
clientId,
redirectUri,
responseType,
scope,
} = props;
const { authority, clientId, redirectUri, responseType, scope } = props;
userManager = new UserManager({
authority,
client_id: clientId,
Expand Down Expand Up @@ -132,6 +134,9 @@ export const AuthProvider: FC<AuthProviderProps> = (props) => {

const user = await userManager!.getUser();
if ((!user || user === null) && autoSignIn) {
if (onBeforeSignIn) {
onBeforeSignIn();
}
signIn();
} else {
setUserData(user);
Expand Down

0 comments on commit e2f609e

Please sign in to comment.