Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-1836] Update state for local logouts #81

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('Auth0Provider', () => {
});

it('should handle errors when checking session', async () => {
clientMock.checkSession.mockRejectedValue({
clientMock.checkSession.mockRejectedValueOnce({
error: '__test_error__',
error_description: '__test_error_description__',
});
Expand Down Expand Up @@ -248,17 +248,41 @@ describe('Auth0Provider', () => {
});

it('should provide a logout method', async () => {
clientMock.isAuthenticated.mockResolvedValue(true);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.logout).toBeInstanceOf(Function);
await result.current.logout({ returnTo: '__return_to__' });
act(() => {
result.current.logout();
});
expect(clientMock.logout).toHaveBeenCalled();
// Should not update state until returned from idp
expect(result.current.isAuthenticated).toBe(true);
});

it('should update state for local logouts', async () => {
clientMock.isAuthenticated.mockResolvedValue(true);
clientMock.getUser.mockResolvedValue('__test_user__');
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.isAuthenticated).toBe(true);
expect(result.current.user).toBe('__test_user__');
act(() => {
result.current.logout({ localOnly: true });
});
expect(clientMock.logout).toHaveBeenCalledWith({
returnTo: '__return_to__',
localOnly: true,
});
expect(result.current.isAuthenticated).toBe(false);
expect(result.current.user).toBeUndefined();
});

it('should provide a getAccessTokenSilently method', async () => {
Expand Down
10 changes: 9 additions & 1 deletion src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Auth0ClientOptions,
CacheLocation,
IdToken,
LogoutOptions,
PopupLoginOptions,
PopupConfigOptions,
RedirectLoginOptions as Auth0RedirectLoginOptions,
Expand Down Expand Up @@ -224,6 +225,13 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
dispatch({ type: 'LOGIN_POPUP_COMPLETE', isAuthenticated, user });
};

const logout = (opts: LogoutOptions = {}): void => {
client.logout(opts);
if (opts.localOnly) {
dispatch({ type: 'LOGOUT' });
}
};

return (
<Auth0Context.Provider
value={{
Expand All @@ -239,7 +247,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
loginWithRedirect: (opts): Promise<void> =>
client.loginWithRedirect(toAuth0LoginRedirectOptions(opts)),
loginWithPopup,
logout: (opts): void => client.logout(opts),
logout,
}}
>
{children}
Expand Down
7 changes: 7 additions & 0 deletions src/reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Action =
isAuthenticated: boolean;
user?: User;
}
| { type: 'LOGOUT' }
| { type: 'ERROR'; error: Error };

/**
Expand All @@ -28,6 +29,12 @@ export const reducer = (state: AuthState, action: Action): AuthState => {
isLoading: false,
error: undefined,
};
case 'LOGOUT':
return {
...state,
isAuthenticated: false,
user: undefined,
};
case 'ERROR':
return {
...state,
Expand Down