added forgotten local auth file
This commit is contained in:
parent
ae9abaf9fd
commit
c549372262
1 changed files with 44 additions and 0 deletions
44
src/providers/authContext.tsx
Normal file
44
src/providers/authContext.tsx
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import { useAuth0 } from "@auth0/auth0-react"
|
||||||
|
import { createContext, PropsWithChildren, useContext } from "react"
|
||||||
|
|
||||||
|
interface IAuthContext {
|
||||||
|
isAuthenticated: boolean
|
||||||
|
loginWithRedirect: () => void
|
||||||
|
logout: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const AuthContext = createContext<IAuthContext>({
|
||||||
|
isAuthenticated: false,
|
||||||
|
loginWithRedirect: () => {},
|
||||||
|
logout: () => {},
|
||||||
|
})
|
||||||
|
|
||||||
|
export function LocalAuthProvider(props: PropsWithChildren<{ token: string }>) {
|
||||||
|
const { children, token } = props
|
||||||
|
const doLogout = () => {
|
||||||
|
localStorage.removeItem('local_auth_token')
|
||||||
|
window.location.href = '/'
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<AuthContext.Provider
|
||||||
|
value={{
|
||||||
|
isAuthenticated: !!token,
|
||||||
|
loginWithRedirect: doLogout,
|
||||||
|
logout: doLogout,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Auth0AuthBridge(props: PropsWithChildren<{}>) {
|
||||||
|
const { isAuthenticated, loginWithRedirect, logout } = useAuth0()
|
||||||
|
return (
|
||||||
|
<AuthContext.Provider value={{ isAuthenticated, loginWithRedirect, logout: () => logout({ logoutParams: { returnTo: window.location.origin } }) }}>
|
||||||
|
{props.children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useAuthContext = () => useContext(AuthContext)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue