-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleAuth.js
More file actions
72 lines (60 loc) · 1.83 KB
/
GoogleAuth.js
File metadata and controls
72 lines (60 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react'
import {connect} from 'react-redux'
import{signIn,signOut} from "../actions";
class GoogleAuth extends React.Component{
componentDidMount() {
window.gapi.load('client:auth2',()=>{
window.gapi.client.init({
clientId:"863859215895-vrfusq433njrt8glg4kr99ora64mtm15.apps.googleusercontent.com",
scope: 'email'
}).then(()=>{
this.auth =window.gapi.auth2.getAuthInstance()
this.onAuthChange(this.auth.isSignedIn.get())
this.auth.isSignedIn.listen(this.onAuthChange)
})
})
}
onAuthChange=(isSignedIn)=>{
if(isSignedIn){
this.props.signIn(this.auth.currentUser.get().getId())
}else{
this.props.signOut()
}
}
onSignInClick =()=>{
this.auth.signIn()
}
onSignOutClick =()=>{
this.auth.signOut()
}
renderAuthButton(){
if (this.props.isSignedIn===null){
return null
}else if(this.props.isSignedIn){
return (
<button
onClick ={this.onSignOutClick}
className ='ui red google button'>
<i className ='google icon'/>Sign Out
</button>
)
}else{
return (
<button
onClick={this.onSignInClick}
className ='ui red google button'>
<i className ="google icon"/>Sign In with Google
</button>
)
}
}
render(){
return (
<div>{this.renderAuthButton()}</div>
)
}
}
const mapStateToProps =(state) =>{
return {isSignedIn :state.auth.isSignedIn}
}
export default connect(mapStateToProps,{signIn,signOut}) (GoogleAuth)