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

feat: added buildAuthorizeUrl and buildLogoutUrl from auth0-spa-js #190

Merged
merged 5 commits into from
Feb 10, 2021
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
4 changes: 4 additions & 0 deletions __mocks__/@auth0/auth0-spa-js.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const handleRedirectCallback = jest.fn(() => ({ appState: {} }));
const buildLogoutUrl = jest.fn();
const buildAuthorizeUrl = jest.fn();
const checkSession = jest.fn();
const getTokenSilently = jest.fn();
const getTokenWithPopup = jest.fn();
Expand All @@ -11,6 +13,8 @@ const logout = jest.fn();

export const Auth0Client = jest.fn(() => {
return {
buildAuthorizeUrl,
buildLogoutUrl,
checkSession,
handleRedirectCallback,
getTokenSilently,
Expand Down
34 changes: 34 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ describe('Auth0Provider', () => {
expect(result.current.error).not.toBeDefined();
});

it('should call through to buildAuthorizeUrl method', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.buildAuthorizeUrl).toBeInstanceOf(Function);

const authOptions = {
redirectUri: '__redirect_uri__',
};
await result.current.buildAuthorizeUrl(authOptions);
expect(clientMock.buildAuthorizeUrl).toHaveBeenCalledWith(authOptions);
});

it('should call through to buildLogoutUrl method', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.buildLogoutUrl).toBeInstanceOf(Function);

const logoutOptions = {
returnTo: '/',
client_id: 'blah',
federated: false,
};
result.current.buildLogoutUrl(logoutOptions);
expect(clientMock.buildLogoutUrl).toHaveBeenCalledWith(logoutOptions);
});

it('should login with a popup', async () => {
clientMock.getUser.mockResolvedValue(undefined);
const wrapper = createWrapper();
Expand Down
25 changes: 25 additions & 0 deletions src/auth0-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
GetTokenWithPopupOptions,
IdToken,
LogoutOptions,
LogoutUrlOptions,
PopupLoginOptions,
PopupConfigOptions,
RedirectLoginOptions as Auth0RedirectLoginOptions,
} from '@auth0/auth0-spa-js';
import { createContext } from 'react';
import { AuthState, initialAuthState } from './auth-state';
Expand Down Expand Up @@ -133,6 +135,27 @@ export interface Auth0ContextInterface extends AuthState {
* [Read more about how Logout works at Auth0](https://auth0.com/docs/logout).
*/
logout: (options?: LogoutOptions) => void;

/**
* ```js
* const authUrl = await buildAuthorizeUrl();
* ```
*
* Builds an `/authorize` URL for loginWithRedirect using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*/
buildAuthorizeUrl: (options?: Auth0RedirectLoginOptions) => Promise<string>;

/**
* ```js
* const logoutUrl = buildLogoutUrl();
* ```
*
* returns a URL to the logout endpoint using the parameters provided as arguments.
* @param options
*/
buildLogoutUrl: (options?: LogoutUrlOptions) => string;
}

/**
Expand All @@ -147,6 +170,8 @@ const stub = (): never => {
*/
const initialContext = {
...initialAuthState,
buildAuthorizeUrl: stub,
buildLogoutUrl: stub,
getAccessTokenSilently: stub,
getAccessTokenWithPopup: stub,
getIdTokenClaims: stub,
Expand Down
14 changes: 14 additions & 0 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CacheLocation,
IdToken,
LogoutOptions,
LogoutUrlOptions,
adamjmcgrath marked this conversation as resolved.
Show resolved Hide resolved
PopupLoginOptions,
PopupConfigOptions,
RedirectLoginOptions as Auth0RedirectLoginOptions,
Expand Down Expand Up @@ -228,6 +229,17 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
})();
}, [client, onRedirectCallback, skipRedirectCallback]);

const buildAuthorizeUrl = useCallback(
(opts?: RedirectLoginOptions): Promise<string> =>
client.buildAuthorizeUrl(toAuth0LoginRedirectOptions(opts)),
[client]
);

const buildLogoutUrl = useCallback(
(opts?: LogoutUrlOptions): string => client.buildLogoutUrl(opts),
[client]
);

const loginWithRedirect = useCallback(
(opts?: RedirectLoginOptions): Promise<void> =>
client.loginWithRedirect(toAuth0LoginRedirectOptions(opts)),
Expand Down Expand Up @@ -311,6 +323,8 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
<Auth0Context.Provider
value={{
...state,
buildAuthorizeUrl,
buildLogoutUrl,
getAccessTokenSilently,
getAccessTokenWithPopup,
getIdTokenClaims,
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
GetIdTokenClaimsOptions,
GetTokenWithPopupOptions,
LogoutOptions,
LogoutUrlOptions,
CacheLocation,
GetTokenSilentlyOptions,
IdToken,
Expand Down