Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react'
- import {connect} from 'react-redux'
- import axios from '../../utils/axios'
- import {secrect_key} from "../../constant";
- import {GoogleSignin} from 'react-native-google-signin'
- import {setAuth, setProfilInfo} from '../../store/actions/subscribe'
- import {addNavigationAnchor} from '../../store/actions/navigation'
- import {
- View,
- Text,
- Button,
- ActivityIndicator,
- Alert
- } from 'react-native'
- import {Connect} from "../../store/actions/user";
- export class SubscribeServiceGoogleView extends React.Component {
- constructor(props) {
- super(props);
- console.log("construct")
- }
- async componentDidMount() {
- this.props.addMainAnchor(this.props.navigation.state.key);
- let config = GoogleSignin.configure({
- scopes: ['https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/youtube'],
- webClientId: "441701662853-jher3b5alu6p9ciig58htqq7i9478pal.apps.googleusercontent.com",
- // androidClientId: "441701662853-v4s4kn3d38ea37ue420907jcjv9emg6l.apps.googleusercontent.com", release code
- // iosClientId: "509236156416-uk1an6lpmep7t3qt1vr14nuec0ehc895.apps.googleusercontent.com",
- offlineAccess: false
- });
- let user = await GoogleSignin.signIn();
- console.log('google Account getted : ', user);
- let password = '';
- if (user) {
- password = secrect_key + user.user.email;
- // gen password
- try {
- let signInGoogle = await axios.post(`/Auths/signin/google`, {
- token: user.idToken,
- password: password
- });
- console.log('Auth with google', signInGoogle);
- if (signInGoogle) {
- this.props.setAuth({email: user.user.email, generated_password: password});
- let login = user.user.givenName + ' ' + user.user.familyName;
- this.props.setProfil({
- email: user.user.email,
- login: login,
- first_name: user.user.givenName,
- last_name: user.user.familyName,
- birth_date: '01/01/1991',
- about_me: login,
- gender: 'male',
- avatar: user.user.photo,
- });
- this.props.navigation.navigate('SelectPreferences', {loginType:'social'});
- }
- } catch (error) {
- console.log('error on getting google account : ', error.response || error);
- if (error.response && error.response.data.error.name === "EMAIL_ALREADY_EXISTS"){
- this.props.Connect(user.user.email, password);
- }else{
- Alert.alert('Unable to subscribe with Google', `Server response : ${error.response ? error.response.data.error.name : 'Connection error'}`);
- this.props.navigation.goBack();
- }
- // if (error.code === statusCodes.SIGN_IN_CANCELLED) {
- //
- // } else if (error.code === statusCodes.IN_PROGRESS) {
- // // operation (f.e. sign in) is in progress already
- // } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
- // // play services not available or outdated
- // } else {
- //
- // }
- }
- }
- // .then((user) => {
- // console.log('google Account getted : ', user);
- // return axios
- // .post(`/Auths/signin/google`, {
- // token: user.idToken,
- // password: password
- // })
- // .then(() => {
- // return Promise.resolve(user)
- // })
- // })
- // .then((user) => {
- // this.props.setProfil({
- // first_name: user.givenName,
- // last_name: user.familyName
- // })
- // this.props.setAuth({email: user.email, generated_password: password});
- // this.props.navigation.navigate("SelectPassword");
- // })
- // .catch((err) => {
- // console.log('error on getting google account : ', err.response || err);
- // Alert.alert('Unable to subscribe with Google', `Server response : ${err.response ? err.response.data.error.name : 'Connection error'}`);
- //
- // // this.props.navigation.goBack();
- // })
- }
- render() {
- return (
- <View style={{flex: 1}}>
- <Text>Connecting...</Text>
- <ActivityIndicator
- animating={true}
- size='large'
- />
- </View>
- )
- }
- }
- const mapStateToProps = (state) => ({
- loading: state.user.loading,
- connected: state.user.connected,
- auto: state.user.auto,
- user: state.user
- });
- const mapDispatchToProps = (dispatch) => ({
- addMainAnchor: (key) => dispatch(addNavigationAnchor("Main", key)),
- setAuth: (auth) => dispatch(setAuth(auth)),
- setProfil: (profil) => dispatch(setProfilInfo(profil)),
- Connect: (email, password) => dispatch(Connect(email, password)),
- })
- export default connect(mapStateToProps, mapDispatchToProps)(SubscribeServiceGoogleView);
Add Comment
Please, Sign In to add comment