Skip to content

Instantly share code, notes, and snippets.

@ayoisaiah
Last active January 6, 2020 21:02
Show Gist options
  • Save ayoisaiah/de7eae878328667cd619c91d728937d8 to your computer and use it in GitHub Desktop.
Save ayoisaiah/de7eae878328667cd619c91d728937d8 to your computer and use it in GitHub Desktop.
auth0.js
import React, { useState, useEffect, useContext } from 'react';
import createAuth0Client from '@auth0/auth0-spa-js';
const DEFAULT_REDIRECT_CALLBACK = () =>
window.history.replaceState({}, document.title, window.location.pathname);
export const Auth0Context = React.createContext();
export const useAuth0 = () => useContext(Auth0Context);
export const Auth0Provider = ({
children,
onRedirectCallback = DEFAULT_REDIRECT_CALLBACK,
...initOptions
}) => {
const [isAuthenticated, setIsAuthenticated] = useState();
const [user, setUser] = useState();
const [auth0Client, setAuth0] = useState();
const [loading, setLoading] = useState(true);
useEffect(() => {
const initAuth0 = async () => {
try {
const auth0FromHook = await createAuth0Client(initOptions);
setAuth0(auth0FromHook);
if (window.location.search.includes('code=')) {
const { appState } = await auth0FromHook.handleRedirectCallback();
onRedirectCallback(appState);
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}
setLoading(false);
} catch (error) {
console.log(error);
}
};
initAuth0();
}, []);
const handleRedirectCallback = async () => {
setLoading(true);
await auth0Client.handleRedirectCallback();
const user = await auth0Client.getUser();
setLoading(false);
setIsAuthenticated(true);
setUser(user);
};
return (
<Auth0Context.Provider
value={{
isAuthenticated,
user,
loading,
handleRedirectCallback,
loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),
logout: (...p) => auth0Client.logout(...p),
}}
>
{children}
</Auth0Context.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment