Skip to content

Instantly share code, notes, and snippets.

@usmansbk
Last active May 29, 2019 11:35
Show Gist options
  • Save usmansbk/0076131075a2b5d290d09b744cefc966 to your computer and use it in GitHub Desktop.
Save usmansbk/0076131075a2b5d290d09b744cefc966 to your computer and use it in GitHub Desktop.
import React from 'react';
import {
GoogleSignin,
statusCodes
} from 'react-native-google-signin';
import SimpleToast from 'react-native-simple-toast';
import Button from './Button';
export default class Container extends React.Component {
state = {
loading: false
};
_signIn = async () => {
this.setState({ loading: true });
try {
await GoogleSignin.hasPlayServices();
const {
idToken,
accessTokenExpirationDate,
user
} = await GoogleSignin.signIn();
// onLogin props calls Auth.federatedSignin({ email, token, expires_at, provider })
await this.props.onLogin({
name: user.name,
email: user.email,
pictureUrl: user.photo,
provider: 'google',
token: idToken,
expires_at: accessTokenExpirationDate
});
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
SimpleToast.show('Login cancelled', SimpleToast.SHORT);
} else if (error.code === statusCodes.IN_PROGRESS) {
SimpleToast.show('Login in progress', SimpleToast.SHORT);
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
SimpleToast.show("Google Play services not available", SimpleToast.SHORT);
} else {
SimpleToast.show("Connection error: " + error.message, SimpleToast.SHORT);
}
this.setState({ loading: false });
}
}
render() {
const { loading } = this.state;
return (
<Button
onPress={this._signIn}
disabled={loading}
loading={loading}
/>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment