-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kazuki.fujisawa
committed
Mar 29, 2021
1 parent
a8b6c43
commit 7e9bbcb
Showing
7 changed files
with
78 additions
and
37 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -21,3 +21,5 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
memo |
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,60 @@ | ||
/* global gapi */ | ||
import React, { useEffect, useRef } from "react"; | ||
import { Button } from "@material-ui/core"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import { signIn, signOut } from "../actions"; | ||
|
||
//ログイン画面 | ||
export const GoogleOAuth = () => { | ||
const user = useSelector((state) => state.user); | ||
const dispatch = useDispatch(); | ||
|
||
let auth = useRef(); | ||
|
||
useEffect(() => { | ||
//OAuth接続のおまじない | ||
window.gapi.load("client:auth2", () => { | ||
window.gapi.client | ||
.init({ | ||
clientId: | ||
"193248965975-lm5drb0mkrhepcipvomamrjamgcfopkq.apps.googleusercontent.com", | ||
scope: "email", | ||
}) | ||
// thenを使用することで、処理が成功した場合のみ、処理を進めることができる | ||
.then(() => { | ||
auth.current = window.gapi.auth2.getAuthInstance(); | ||
console.log(auth); | ||
onAuthChange(auth.current.isSignedIn.get()); | ||
// Listen for changes | ||
this.auth.isSignedIn.listen(() => { | ||
onAuthChange(auth.current.isSignedIn.get()); | ||
}); | ||
}); | ||
}); | ||
console.log(auth.current); | ||
}, []); | ||
|
||
const onLoginButtonClick = (action) => { | ||
if (action === "login") auth.current.signIn(); | ||
else auth.current.signOut(); | ||
}; | ||
|
||
const onAuthChange = (isSignedIn) => { | ||
//useのsignのstateをdispatchから更新 | ||
if (isSignedIn) { | ||
const { Eea, ig, U3 } = auth.current.currentUser.get().getBasicProfile(); | ||
dispatch(signIn({ userId: Eea, userName: ig, userEmail: U3 })); | ||
} else dispatch(signOut(auth.current.currentUser.get().getId())); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{user.isSignedIn ? ( | ||
<Button onClick={() => onLoginButtonClick("logout")}>Sign Out</Button> | ||
) : ( | ||
<Button onClick={() => onLoginButtonClick("login")}>Sign In</Button> | ||
)} | ||
</div> | ||
); | ||
}; |
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