Skip to content

Instantly share code, notes, and snippets.

@KendallWeihe
Created December 21, 2019 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KendallWeihe/e307532e8bcf0eb6d99d387f6eab4e67 to your computer and use it in GitHub Desktop.
Save KendallWeihe/e307532e8bcf0eb6d99d387f6eab4e67 to your computer and use it in GitHub Desktop.
TypeScript, React, Hooks & auth0-spa-js
import * as React from "react";
import { useState, useEffect, useContext, FC } from "react";
import createAuth0Client from "@auth0/auth0-spa-js";
interface Auth0ProviderProps {
auth0Options: Auth0ClientOptions;
onRedirectCallback: Function;
}
export const Auth0Context = React.createContext(undefined);
export const useAuth0 = () => useContext(Auth0Context);
export const Auth0Provider: FC<Auth0ProviderProps> = props => {
const [isAuthenticated, setIsAuthenticated] = useState();
const [user, setUser] = useState();
const [auth0Client, setAuth0] = useState();
const [loading, setLoading] = useState(true);
const [popupOpen, setPopupOpen] = useState(false);
useEffect(() => {
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(props.auth0Options);
setAuth0(auth0FromHook);
if (window.location.search.includes("code=")) {
await auth0FromHook.handleRedirectCallback();
props.onRedirectCallback();
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}
setLoading(false);
};
initAuth0();
// eslint-disable-next-line
}, []);
const loginWithPopup = async (params = {}) => {
setPopupOpen(true);
try {
await auth0Client.loginWithPopup(params);
} catch (error) {
console.error(error);
} finally {
setPopupOpen(false);
}
const user = await auth0Client.getUser();
setUser(user);
setIsAuthenticated(true);
};
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,
popupOpen,
loginWithPopup,
handleRedirectCallback,
getIdTokenClaims: (...p: any) => auth0Client.getIdTokenClaims(...p),
loginWithRedirect: (...p: any) => auth0Client.loginWithRedirect(...p),
getTokenSilently: (...p: any) => auth0Client.getTokenSilently(...p),
getTokenWithPopup: (...p: any) => auth0Client.getTokenWithPopup(...p),
logout: (...p: any) => auth0Client.logout(...p)
}}
>
{props.children}
</Auth0Context.Provider>
);
};
@chrisaddams
Copy link

chrisaddams commented Jan 16, 2020

I tried to run this and got the following error:

TypeScript error in /Users/chrisaddams/Development/Auth0-POC/auth0-poc/src/react-auth0-spa.tsx(68,7): Type '{ isAuthenticated: any; user: any; loading: boolean; popupOpen: boolean; loginWithPopup: (params?: {}) => Promise<void>; handleRedirectCallback: () => Promise<void>; getIdTokenClaims: (...p: any) => any; loginWithRedirect: (...p: any) => any; getTokenSilently: (...p: any) => any; getTokenWithPopup: (...p: any) => an...' is not assignable to type 'undefined'.

@romixch
Copy link

romixch commented Feb 5, 2020

Same problem here. My working example is over there: https://gist.github.com/romixch/05728f0adc600d442d6ec38876452d56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment