Skip to content

Commit

Permalink
feat: add option to use signoutRedirect
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre committed May 15, 2020
1 parent ada396a commit d2c4278
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

* [AuthContextProps](interfaces/authcontextprops.md)
* [AuthProviderProps](interfaces/authproviderprops.md)
* [AuthProviderSignOutProps](interfaces/authprovidersignoutprops.md)
* [Location](interfaces/location.md)

### Variables
Expand Down
8 changes: 7 additions & 1 deletion docs/interfaces/authcontextprops.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ ___

#### Type declaration:

▸ (): *void*
▸ (`options?`: [AuthProviderSignOutProps](authprovidersignoutprops.md)): *void*

**Parameters:**

Name | Type |
------ | ------ |
`options?` | [AuthProviderSignOutProps](authprovidersignoutprops.md) |

___

Expand Down
33 changes: 33 additions & 0 deletions docs/interfaces/authprovidersignoutprops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[oidc-react](../README.md)[AuthProviderSignOutProps](authprovidersignoutprops.md)

# Interface: AuthProviderSignOutProps

## Hierarchy

* **AuthProviderSignOutProps**

## Index

### Properties

* [signoutRedirect](authprovidersignoutprops.md#optional-signoutredirect)

## Properties

### `Optional` signoutRedirect

**signoutRedirect**? : *boolean | unknown*

Trigger a redirect of the current window to the end session endpoint

You can also provide an object. This object will be sent with the
function.

**`example`**
```javascript
const config = {
signOutRedirect: {
state: 'abrakadabra',
},
};
```
31 changes: 27 additions & 4 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ export interface Location {
hash: string;
}

export interface AuthProviderSignOutProps {
/**
* Trigger a redirect of the current window to the end session endpoint
*
* You can also provide an object. This object will be sent with the
* function.
*
* @example
* ```javascript
* const config = {
* signOutRedirect: {
* state: 'abrakadabra',
* },
* };
* ```
*/
signoutRedirect?: boolean | unknown;
}

export interface AuthProviderProps {
/**
* See [UserManager](https://github.com/IdentityModel/oidc-client-js/wiki#usermanager) for more details.
Expand Down Expand Up @@ -56,12 +75,12 @@ export interface AuthProviderProps {
/**
* On sign out hook. Can be a async function.
*/
onSignOut?: () => Promise<void> | void;
onSignOut?: (options?: AuthProviderSignOutProps) => Promise<void> | void;
}

export interface AuthContextProps {
signIn: () => void;
signOut: (providerLogout?: boolean) => void;
signOut: (options?: AuthProviderSignOutProps) => void;
/**
* See [User](https://github.com/IdentityModel/oidc-client-js/wiki#user) for more details.
*/
Expand Down Expand Up @@ -148,8 +167,12 @@ export const AuthProvider: FC<AuthProviderProps> = (props) => {

const context: AuthContextProps = {
signIn,
signOut: async (providerLogout?: boolean) => {
providerLogout ? await userManager!.signoutRedirect() : await userManager!.removeUser();
signOut: async (options) => {
if (options && options?.signoutRedirect) {
await userManager!.signoutRedirect()
} else {
await userManager!.removeUser();
}
setUserData(null);
onSignOut && onSignOut();
},
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/AuthContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe('AuthContext', () => {
>
<AuthContext.Consumer>
{(value) => {
value.signOut(true);
value.signOut({
signoutRedirect: true
});
return <p>Bjerk</p>;
}}
</AuthContext.Consumer>
Expand Down

0 comments on commit d2c4278

Please sign in to comment.