通过Google登录名传递用户名和姓氏

问题描述 投票:0回答:1

我设法使用'expo-google-app-auth'设置了Google登录名。这是我的Login.js,它处理登录页面以及google登录详细信息和逻辑:

import React, { Component } from 'react'
import {Text,
        View,
        StyleSheet,
        Image,
        Alert} from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler';
import { Provider as PaperProvider, 
         Button, 
         Caption} from 'react-native-paper'
import { createStackNavigator } from '@react-navigation/stack'
import { NavigationContainer } from '@react-navigation/native';


import MyDrawer from '../navigation/Drawer'
import QR from './QR'

const Stack = createStackNavigator();

import * as Google from 'expo-google-app-auth';

const IOS_CLIENT_ID =
  "your-ios-client-id";
const ANDROID_CLIENT_ID =
  "my-android-id";

class Login extends Component {  

    signInWithGoogle = async () => {
        try {
          const result = await Google.logInAsync({
            iosClientId: IOS_CLIENT_ID,
            androidClientId: ANDROID_CLIENT_ID,
            scopes: ["profile", "email"]
          });

          if (result.type === "success") {
            console.log("LoginScreen.js.js 21 | ", result.user.givenName, result.user.familyName, result.user.email, result.user.photoUrl);
            this.props.navigation.navigate("MyDrawer", {
              username: result.user.givenName,
              lastname: result.user.familyName,
              email: result.user.email,
              photoUrl: result.user.photoUrl
            }); //after Google login redirect to MyDrawer
            return result.accessToken;
          } else {
            return { cancelled: true };
          }
        } catch (e) {
          console.log('LoginScreen.js.js 30 | Error with login', e);
          return { error: true };
        }
      };
render(){
        return (
            <PaperProvider>
                <View style={styles.container}>
                    <View style={styles.imageCon}>
                        <Image
                        source={require('../img/logo-krug.png')}></Image>
                    </View>
                    <View style={styles.textCon}>
                        <Text style={styles.text}> Dobrodošli u eSTUDENT mobilnu aplikaciju!</Text>
                    </View>
                    <View style={styles.buttonCon}>
                        <Button
                        icon='camera'
                        mode='outlined'
                        onPress={this.signInWithGoogle}
                        >   
                        google
                        </Button>
                    </View>
                </View>
            </PaperProvider>
        )
}

}
 export default function LoginStack() {
     return (
         <NavigationContainer>
             <Stack.Navigator
                 initialRouteName='Login'
                 screenOptions={{
                        headerShown: false
                    }}>
                 <Stack.Screen name='MyDrawer' component={MyDrawer} />
                 <Stack.Screen name='Login' component={Login} />
             </Stack.Navigator>
         </NavigationContainer>
     )
 }

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#ecf0f1'
    },
    imageCon: {
        height: '35%',
        width: '100%',
        justifyContent: "center",
        alignItems: "center"
    },
    textCon:{
        height:'20%',
        alignItems: 'center',
        justifyContent: 'center'
    },
    text: {
        textAlign: "center",
        fontSize: 20,
        fontWeight: 'bold'
    },
    buttonCon:{
        height: '30%',
        justifyContent:'center',
        alignItems: 'center'
    },
});

而且我想将用户名,姓氏,电子邮件和个人资料图片传递给这两个文件。Drawer.js,用于处理侧面的Drawer和导航:

import React from 'react'
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { Ionicons, MaterialIcons, Feather } from '@expo/vector-icons';
import { ScrollView,
         TouchableOpacity,
         Text,
         View,
         Image,
         StatusBar,
         StyleSheet,
          } from 'react-native'

import QR from '../screens/QR';
import Odabir from '../screens/Odabir'
import { SafeAreaView } from 'react-navigation';
import { Divider } from 'react-native-paper'


const Drawer = createDrawerNavigator();

function CustomDrawerContent(props) {
  return(
    <SafeAreaView style={{flex:1}}>
    <View style={{height: 150, alignItems: 'center', justifyContent: 'center', marginTop: Platform.OS === 'ios' ? 0 : StatusBar.currentHeight}}>
      <Image source={require('../img/account.png')}
        style={{height: 120, width: 120, borderRadius: 60}}
       />
    </View>
    <View style={{height: 50, alignItems:'center'}}>
      <Text style={styles.naslov}> This is where I want to pass the username and last name </Text>
    </View>
    <Divider />
      <ScrollView style={{marginLeft: 5,}}>

        <TouchableOpacity
          style={{marginTop: 20, marginLeft: 20}}
          onPress={() => props.navigation.navigate('QR')}
          >
        <View style={{padding:10}}>
          <Ionicons name='ios-qr-scanner' size={20} styles={{}}>
            <Text style={styles.naslov}>      QR</Text>
          </Ionicons>
          </View>
          </TouchableOpacity>
          <TouchableOpacity
          style={{marginTop: 20, marginLeft: 20}}
          onPress={() => props.navigation.navigate('Odabir')}
          >
        <View style={{padding:10}}>
          <Ionicons name='ios-qr-scanner' size={20} styles={{}}>
            <Text style={styles.naslov}>      Odabir</Text>
          </Ionicons>
          </View>
          </TouchableOpacity>
          <TouchableOpacity
          style={{marginTop: 20, marginLeft: 20}}
          onPress={() => props.navigation.navigate('Odabir')}
          >
        <View style={{padding:10}}>
          <Ionicons name='ios-qr-scanner' size={20} styles={{}}>
            <Text style={styles.naslov}>      Odabir</Text>
          </Ionicons>
          </View>
          </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  )
}



export default function MyDrawer() {
  return (
      <NavigationContainer independent={true}>
        <Drawer.Navigator initialRouteName='QR' drawerContent={props => CustomDrawerContent(props)}>
        <Drawer.Screen
         name="QR" 
         component={QR}
          />
        <Drawer.Screen 
         name="Odabir" 
         component={Odabir}
          />
        </Drawer.Navigator>
    </NavigationContainer>
  );
}

const styles = StyleSheet.create({
  naslov: {
    fontSize: 20,
  },

})

最后是QR.js,它包含一些基本信息,由于它是用户位于的“主页”页面,因此我想对其进行个性化设置

import React, { Component } from 'react'
import {Text,
        SafeAreaView,
        View, 
        StyleSheet,
        Image 
        } from 'react-native'

import Header from '../navigation/Header'

export default function QR({navigation}) {
        return (
            <SafeAreaView style={styles.container}>
            <Header title='Moj QR' isHome={true} navigation={navigation}/>
                <View style={styles.qrCon}>
                    <Image
                        style={styles.qr} 
                        source={require('../img/QR_code_for_mobile_English_Wikipedia.svg.png')}
                        />
                </View>
                <View style={styles.textCon}>
                    <Text style={styles.text}>This is where I want to pass the username and last name</Text>
                    <Text style={styles.text}>Ovo je tvoj QR kod</Text>
                </View>

            </SafeAreaView>
        )
    }

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#ecf0f1',
        alignItems: 'center',

    },
    qrCon:{
        width: '60%',
        height: '60%',
        alignItems: 'center',
        justifyContent: 'center',
    },
    qr: {
        width: '120%',
        height: '120%',
        resizeMode: 'contain'
    },
    textCon: {
        height: '7%'
    },
    text:{
        fontSize: 35,
        fontWeight: '600'
    },
    buttonCon:{
        height: '15%',
        justifyContent:'center',
        alignItems: 'center'
    }
})

我正在跟踪的示例使用了{this.props.navigation.getParam("username")},但对我来说不起作用。我将如何准确地传递数据?

reactjs react-native oauth expo google-login
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.