|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
2 | 2 | import { gapi, loadAuth2 } from 'gapi-script' |
3 | 3 |
|
4 | 4 | import UserCard from './UserCard'; |
5 | 5 | import './GoogleLogin.css'; |
6 | 6 |
|
7 | | -class GoogleLogin extends Component { |
8 | | - constructor(props) { |
9 | | - super(props); |
10 | | - this.state = { |
11 | | - user: null |
12 | | - } |
13 | | - } |
14 | | - async componentDidMount() { |
15 | | - let auth2 = await loadAuth2(process.env.REACT_APP_CLIENT_ID, '') |
16 | | - if (auth2.isSignedIn.get()) { |
17 | | - this.updateUser(auth2.currentUser.get()) |
18 | | - } else { |
19 | | - this.attachSignin(document.getElementById('customBtn'), auth2); |
20 | | - } |
21 | | - } |
22 | | - async componentDidUpdate() { |
23 | | - if(!this.state.user) { |
24 | | - let auth2 = await loadAuth2(process.env.REACT_APP_CLIENT_ID, '') |
25 | | - this.attachSignin(document.getElementById('customBtn'), auth2); |
26 | | - } |
27 | | - } |
28 | | - updateUser(currentUser) { |
29 | | - let name = currentUser.getBasicProfile().getName() |
30 | | - let profileImg = currentUser.getBasicProfile().getImageUrl() |
31 | | - this.setState({ |
32 | | - user: { |
33 | | - name: name, |
34 | | - profileImg: profileImg |
35 | | - } |
36 | | - }) |
37 | | - } |
38 | | - attachSignin(element, auth2) { |
39 | | - auth2.attachClickHandler(element, {}, |
40 | | - (googleUser) => { |
41 | | - this.updateUser(googleUser); |
42 | | - }, (error) => { |
43 | | - console.log(JSON.stringify(error)) |
44 | | - }); |
45 | | - } |
46 | | - signOut = () => { |
47 | | - let auth2 = gapi.auth2.getAuthInstance(); |
48 | | - auth2.signOut().then(() => { |
49 | | - this.setState({ user: null }) |
50 | | - console.log('User signed out.'); |
51 | | - }); |
| 7 | +export const GoogleLogin = () => { |
| 8 | + const [user, setUser] = useState(null); |
| 9 | + |
| 10 | + useEffect(async () => { |
| 11 | + const auth2 = await loadAuth2(process.env.REACT_APP_CLIENT_ID, '') |
| 12 | + if (auth2.isSignedIn.get()) { |
| 13 | + updateUser(auth2.currentUser.get()) |
| 14 | + } else { |
| 15 | + attachSignin(document.getElementById('customBtn'), auth2); |
52 | 16 | } |
53 | | - render() { |
54 | | - if(this.state.user) { |
55 | | - return ( |
56 | | - <div className="container"> |
57 | | - <UserCard user={this.state.user} /> |
58 | | - <div id="" className="btn logout" onClick={this.signOut}> |
59 | | - Logout |
60 | | - </div> |
61 | | - </div> |
62 | | - ); |
63 | | - } else { |
64 | | - return ( |
65 | | - <div className="container"> |
66 | | - <div id="customBtn" className="btn login"> |
67 | | - Login |
68 | | - </div> |
69 | | - </div> |
70 | | - ); |
71 | | - } |
| 17 | + }, []); |
| 18 | + |
| 19 | + useEffect(async () => { |
| 20 | + if (!user) { |
| 21 | + const auth2 = await loadAuth2(process.env.REACT_APP_CLIENT_ID, '') |
| 22 | + attachSignin(document.getElementById('customBtn'), auth2); |
72 | 23 | } |
73 | | -} |
| 24 | + }, [user]) |
| 25 | + |
| 26 | + const updateUser = (currentUser) => { |
| 27 | + const name = currentUser.getBasicProfile().getName(); |
| 28 | + const profileImg = currentUser.getBasicProfile().getImageUrl(); |
| 29 | + setUser({ |
| 30 | + name: name, |
| 31 | + profileImg: profileImg, |
| 32 | + }); |
| 33 | + }; |
| 34 | + |
| 35 | + const attachSignin = (element, auth2) => { |
| 36 | + auth2.attachClickHandler(element, {}, |
| 37 | + (googleUser) => { |
| 38 | + updateUser(googleUser); |
| 39 | + }, (error) => { |
| 40 | + console.log(JSON.stringify(error)) |
| 41 | + }); |
| 42 | + }; |
74 | 43 |
|
75 | | -export default GoogleLogin; |
| 44 | + const signOut = () => { |
| 45 | + const auth2 = gapi.auth2.getAuthInstance(); |
| 46 | + auth2.signOut().then(() => { |
| 47 | + setUser(null); |
| 48 | + console.log('User signed out.'); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + if(user) { |
| 53 | + return ( |
| 54 | + <div className="container"> |
| 55 | + <UserCard user={user} /> |
| 56 | + <div id="" className="btn logout" onClick={signOut}> |
| 57 | + Logout |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="container"> |
| 65 | + <div id="customBtn" className="btn login"> |
| 66 | + Login |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + ); |
| 70 | +} |
0 commit comments