This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from SoyBeansLab/feature/auth0
認証周りの実装
- Loading branch information
Showing
8 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { makeStyles } from "@material-ui/core/styles"; | ||
import Button from "@material-ui/core/Button"; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
button: { | ||
marginRight: theme.spacing(2), | ||
color: "#ffffff", | ||
}, | ||
})); | ||
|
||
export default function LinkButton(props) { | ||
const style = useStyles(); | ||
const text = props.text || ""; | ||
const onClick = props.onClick || function() {}; | ||
|
||
return ( | ||
<Button color="inherit" className={style} onClick={onClick}> | ||
{text} | ||
</Button> | ||
); | ||
} | ||
|
||
LinkButton.propTypes = { | ||
style: PropTypes.object, | ||
text: PropTypes.string, | ||
onClick: PropTypes.func, | ||
}; |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
https://github.com/auth0-samples/auth0-react-samples/blob/master/01-Login/src/react-auth0-spa.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); | ||
// eslint-disable-next-line | ||
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); | ||
const [popupOpen, setPopupOpen] = useState(false); | ||
|
||
useEffect(() => { | ||
const initAuth0 = async () => { | ||
const auth0FromHook = await createAuth0Client(initOptions); | ||
setAuth0(auth0FromHook); | ||
|
||
if (window.location.search.includes("code=") && window.location.search.includes("state=")) { | ||
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); | ||
}; | ||
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) => auth0Client.getIdTokenClaims(...p), | ||
loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p), | ||
getTokenSilently: (...p) => auth0Client.getTokenSilently(...p), | ||
getTokenWithPopup: (...p) => auth0Client.getTokenWithPopup(...p), | ||
logout: (...p) => auth0Client.logout(...p), | ||
}} | ||
> | ||
{children} | ||
</Auth0Context.Provider> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { createBrowserHistory } from "history"; | ||
export default createBrowserHistory(); |