Skip to content

Commit 0e1593d

Browse files
author
Oliver Blum
authored
app.js
Google Sign in with Firebase https://github.com/fullstackreact/react-native-firestack https://github.com/devfd/react-native-google-signin 17/11/2016 As both modules are a work in progress, this example worked for me. You may notice that I did not use "firestack.auth.listenForAuth" to listen to the auth changes. Well, I found that slow and kind of out of sync. This pull request: fullstackreact/react-native-firestack#132 fixed few things and the "signInWithProvider" is now responding with the correct user object. Anyway, for future reference...
1 parent da6a4c7 commit 0e1593d

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

app.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import React, {Component} from 'react'
2+
import {
3+
StyleSheet,
4+
Text,
5+
TouchableOpacity,
6+
View,
7+
Navigator,
8+
ActivityIndicator
9+
} from 'react-native'
10+
11+
import Firestack from 'react-native-firestack'
12+
import {GoogleSignin, GoogleSigninButton} from 'react-native-google-signin'
13+
14+
const FirebaseStack = new Firestack({debug: true})
15+
16+
class myapp extends Component {
17+
18+
constructor(props) {
19+
super(props)
20+
this.state = {
21+
user: null,
22+
loading: false
23+
}
24+
}
25+
26+
componentDidMount() {
27+
this.setupGoogleSignIn();
28+
}
29+
30+
// after google signIn is done, this function will run
31+
firebaseLogin(user) {
32+
if(user.idToken) {
33+
FirebaseStack.auth.signInWithProvider('google', user.idToken).then((userFirebase) => {
34+
let userData = userFirebase.user
35+
if(userData) {
36+
userData.googleId = user.id
37+
}
38+
39+
this.setState({user: userData, loading: false})
40+
41+
}).catch((error) => {
42+
console.log(error)
43+
});
44+
}
45+
}
46+
47+
async setupGoogleSignIn() {
48+
try {
49+
this.setState({loading: true});
50+
51+
await GoogleSignin.hasPlayServices({ autoResolve: true });
52+
await GoogleSignin.configure({
53+
iosClientId: '409486988658-04julm8idncs3vlqpdboc6a0napgntit.apps.googleusercontent.com',
54+
webClientId: '409486988658-r57kps5v4kfl2gbvktq6up916oauc8hj.apps.googleusercontent.com',
55+
offlineAccess: false // if this is true, the user object will not come back with an idToken but Server key token
56+
// if it is false, the object will come back with a server key = null
57+
});
58+
59+
GoogleSignin.currentUserAsync().then((user)=> {
60+
if(user && user.idToken) {
61+
// log in to firebase
62+
this.firebaseLogin(user)
63+
} else {
64+
this.setState({loading: false});
65+
}
66+
})
67+
.catch((error) => {
68+
console.log(error)
69+
})
70+
}
71+
72+
catch(err) {
73+
this.setState({loading: false});
74+
console.log("Google signin error", err.code, err.message);
75+
}
76+
}
77+
78+
signIn = () => {
79+
this.setState({loading: true});
80+
GoogleSignin.signIn()
81+
.then((user) => {
82+
this.setState({loading: false, user: user})
83+
console.log(user)
84+
})
85+
.catch((err) => {
86+
console.log('WRONG SIGNIN', err)
87+
})
88+
.done();
89+
}
90+
91+
signOut() {
92+
this.setState({loading: true})
93+
GoogleSignin.revokeAccess()
94+
.then(() => GoogleSignin.signOut())
95+
.then(() => {this.setState({user: null, loading: false}) } )
96+
.done()
97+
}
98+
99+
render() {
100+
if(this.state.loading) {
101+
return (
102+
<View>
103+
<ActivityIndicator
104+
animating={true}
105+
style={{height: 80}}
106+
size="large"
107+
/>
108+
</View>
109+
);
110+
} else {
111+
if (!this.state.user) {
112+
return (
113+
<View>
114+
<TouchableOpacity onPress={() => {this.signIn()} } style={{width: 200, height: 44, backgroundColor: '#ccc'}}>
115+
<Text>Login</Text>
116+
</TouchableOpacity>
117+
</View>
118+
)
119+
}
120+
else {
121+
return (
122+
<View>
123+
<Text>Hello {this.state.user.displayName} {this.state.user.uid} </Text>
124+
<TouchableOpacity onPress={() => {this.signOut()} } style={{width: 200, height: 44, backgroundColor: '#ccc'}}>
125+
<Text>Logout</Text>
126+
</TouchableOpacity>
127+
</View>
128+
129+
)
130+
}
131+
}
132+
}
133+
}
134+
135+
module.exports = myapp

0 commit comments

Comments
 (0)