Guest User

SubscribeServiceGoogle

a guest
Feb 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import {connect} from 'react-redux'
  3. import axios from '../../utils/axios'
  4. import {secrect_key} from "../../constant";
  5.  
  6. import {GoogleSignin} from 'react-native-google-signin'
  7.  
  8. import {setAuth, setProfilInfo} from '../../store/actions/subscribe'
  9. import {addNavigationAnchor} from '../../store/actions/navigation'
  10.  
  11. import {
  12.     View,
  13.     Text,
  14.     Button,
  15.     ActivityIndicator,
  16.     Alert
  17. } from 'react-native'
  18. import {Connect} from "../../store/actions/user";
  19.  
  20. export class SubscribeServiceGoogleView extends React.Component {
  21.  
  22.     constructor(props) {
  23.         super(props);
  24.         console.log("construct")
  25.     }
  26.  
  27.     async componentDidMount() {
  28.  
  29.         this.props.addMainAnchor(this.props.navigation.state.key);
  30.         let config = GoogleSignin.configure({
  31.             scopes: ['https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/youtube'],
  32.             webClientId: "441701662853-jher3b5alu6p9ciig58htqq7i9478pal.apps.googleusercontent.com",
  33.             // androidClientId: "441701662853-v4s4kn3d38ea37ue420907jcjv9emg6l.apps.googleusercontent.com",  release code
  34.             // iosClientId: "509236156416-uk1an6lpmep7t3qt1vr14nuec0ehc895.apps.googleusercontent.com",
  35.             offlineAccess: false
  36.         });
  37.         let user = await GoogleSignin.signIn();
  38.         console.log('google Account getted : ', user);
  39.         let password = '';
  40.  
  41.         if (user) {
  42.             password = secrect_key + user.user.email;
  43.             // gen password
  44.             try {
  45.  
  46.                 let signInGoogle = await axios.post(`/Auths/signin/google`, {
  47.                     token: user.idToken,
  48.                     password: password
  49.                 });
  50.  
  51.                 console.log('Auth with google', signInGoogle);
  52.  
  53.                 if (signInGoogle) {
  54.                     this.props.setAuth({email: user.user.email, generated_password: password});
  55.  
  56.                     let login = user.user.givenName + ' ' + user.user.familyName;
  57.  
  58.                     this.props.setProfil({
  59.                         email: user.user.email,
  60.                         login: login,
  61.                         first_name: user.user.givenName,
  62.                         last_name: user.user.familyName,
  63.                         birth_date: '01/01/1991',
  64.                         about_me: login,
  65.                         gender: 'male',
  66.                         avatar: user.user.photo,
  67.                     });
  68.                     this.props.navigation.navigate('SelectPreferences', {loginType:'social'});
  69.                 }
  70.             } catch (error) {
  71.                 console.log('error on getting google account : ', error.response || error);
  72.                 if (error.response && error.response.data.error.name === "EMAIL_ALREADY_EXISTS"){
  73.                     this.props.Connect(user.user.email, password);
  74.                 }else{
  75.                     Alert.alert('Unable to subscribe with Google', `Server response : ${error.response ? error.response.data.error.name : 'Connection error'}`);
  76.                     this.props.navigation.goBack();
  77.                 }
  78.  
  79.                 // if (error.code === statusCodes.SIGN_IN_CANCELLED) {
  80.                 //
  81.                 // } else if (error.code === statusCodes.IN_PROGRESS) {
  82.                 //     // operation (f.e. sign in) is in progress already
  83.                 // } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
  84.                 //     // play services not available or outdated
  85.                 // } else {
  86.                 //
  87.                 // }
  88.             }
  89.  
  90.         }
  91.  
  92.         // .then((user) => {
  93.         //  console.log('google Account getted : ', user);
  94.         //  return axios
  95.         //          .post(`/Auths/signin/google`, {
  96.         //              token: user.idToken,
  97.         //              password: password
  98.         //          })
  99.         //          .then(() => {
  100.         //              return Promise.resolve(user)
  101.         //          })
  102.         // })
  103.         // .then((user) => {
  104.         //  this.props.setProfil({
  105.         //      first_name: user.givenName,
  106.         //      last_name: user.familyName
  107.         //  })
  108.         //  this.props.setAuth({email: user.email, generated_password: password});
  109.         //  this.props.navigation.navigate("SelectPassword");
  110.         // })
  111.         // .catch((err) => {
  112.         //  console.log('error on getting google account : ', err.response || err);
  113.         //  Alert.alert('Unable to subscribe with Google', `Server response : ${err.response ? err.response.data.error.name : 'Connection error'}`);
  114.         //
  115.         //  // this.props.navigation.goBack();
  116.         // })
  117.     }
  118.  
  119.     render() {
  120.  
  121.         return (
  122.             <View style={{flex: 1}}>
  123.                 <Text>Connecting...</Text>
  124.                 <ActivityIndicator
  125.                     animating={true}
  126.                     size='large'
  127.                 />
  128.             </View>
  129.         )
  130.     }
  131.  
  132. }
  133.  
  134. const mapStateToProps = (state) => ({
  135.     loading: state.user.loading,
  136.     connected: state.user.connected,
  137.     auto: state.user.auto,
  138.     user: state.user
  139. });
  140.  
  141. const mapDispatchToProps = (dispatch) => ({
  142.     addMainAnchor: (key) => dispatch(addNavigationAnchor("Main", key)),
  143.     setAuth: (auth) => dispatch(setAuth(auth)),
  144.     setProfil: (profil) => dispatch(setProfilInfo(profil)),
  145.     Connect: (email, password) => dispatch(Connect(email, password)),
  146. })
  147.  
  148. export default connect(mapStateToProps, mapDispatchToProps)(SubscribeServiceGoogleView);
Add Comment
Please, Sign In to add comment