Created
December 21, 2019 19:32
-
-
Save KendallWeihe/e307532e8bcf0eb6d99d387f6eab4e67 to your computer and use it in GitHub Desktop.
TypeScript, React, Hooks & auth0-spa-js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same problem here. My working example is over there: https://gist.github.com/romixch/05728f0adc600d442d6ec38876452d56