Skip to content

Commit b94bad1

Browse files
committed
Update GoogleLogin to be a functional component
1 parent c12ac5c commit b94bad1

File tree

3 files changed

+69
-74
lines changed

3 files changed

+69
-74
lines changed

src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import GoogleLogin from './components/GoogleLogin';
3+
import { GoogleLogin } from './components/GoogleLogin';
44
import './App.css';
55

66
function App() {

src/components/GoogleLogin.js

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,70 @@
1-
import React, { Component } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { gapi, loadAuth2 } from 'gapi-script'
33

44
import UserCard from './UserCard';
55
import './GoogleLogin.css';
66

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);
5216
}
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);
7223
}
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+
};
7443

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+
}

src/components/UserCard.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22

33
const UserCard = (props) => {
4-
return (
5-
<div>
6-
<h2>{props.user.name}</h2>
7-
<img src={props.user.profileImg} alt="user profile" />
8-
</div>
9-
);
4+
return (
5+
<div>
6+
<h2>{props.user.name}</h2>
7+
<img src={props.user.profileImg} alt="user profile" />
8+
</div>
9+
);
1010
}
1111

1212
export default UserCard;

0 commit comments

Comments
 (0)