Skip to content

Instantly share code, notes, and snippets.

@chaitanya-bhagavan
Last active November 14, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chaitanya-bhagavan/4439f225d958fbad423349e72ef570da to your computer and use it in GitHub Desktop.
Save chaitanya-bhagavan/4439f225d958fbad423349e72ef570da to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import {
Text,
View,
TouchableOpacity,
ActivityIndicator,
Navigator
} from 'react-native';
import {GoogleSignin} from 'react-native-google-signin';
import Firestack from 'react-native-firestack'
const firestack = new Firestack({debug: true});
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
user: null,
loading: false
};
}
componentDidMount() {
this.setupGoogleSignIn();
}
componentWillMount() {
console.log('a');
}
async setupGoogleSignIn() {
try {
this.setState({loading: true});
await GoogleSignin.hasPlayServices({ autoResolve: true });
await GoogleSignin.configure({
iosClientId: '<IOS-CLIENTID>',
webClientId: '<WEB-CLIENTID>',
offlineAccess: true
});
GoogleSignin.currentUserAsync().then((user)=> {
if(user) {
console.log('GoogleUser ', user);
this.setState({loading: false, user: user});
} else {
this.setState({loading: false});
}
});
firestack.auth.listenForAuth((evt)=> {
if (!evt.authenticated) {
console.log(evt.error)
} else {
console.log('firebase user ', evt.user);
}
})
.then(() => console.log('Listening for authentication changes'));
}
catch(err) {
this.setState({loading: false});
console.log("Google signin error", err.code, err.message);
}
}
signIn() {
this.setState({loading: true});
GoogleSignin.signIn()
.then((user) => {
this.setState({loading: false, user: user});
}).catch((err) => {
console.log('WRONG SIGNIN', err);
})
.done();
}
signOut() {
this.setState({loading: true});
GoogleSignin.revokeAccess().then(() => GoogleSignin.signOut()).then(() => {
this.setState({user: null, loading: false});
})
.done();
}
render() {
if(this.state.loading) {
return (
<View>
<ActivityIndicator
animating={true}
style={{height: 80}}
size="large"
/>
</View>
);
} else {
if (!this.state.user) {
return (
<View>
<TouchableOpacity onPress={this.signIn.bind(this)} >
<Text>Login</Text>
</TouchableOpacity>
</View>
);
}
else {
firestack.signInWithProvider('google', this.state.user.idToken).then((userFirebase)=>{
console.log(userFirebase)
}).catch((error)=> {
console.log(error.error)
});
console.log('logged in user ', this.state.user);
return (
<Text>{this.state.user.displayName}</Text>
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment