我的应用程序应该做的是使用google oauth进行登录,然后服务器将返回一个创建用户(如果尚未创建用户)并登录用户(通过创建会话)。
我确定我的后端工作正常,因为我已经用邮递员测试了所有内容。 但是,我无法处理用户的创建(如果尚未创建用户)和console.log我的jwt以确保自己可以正常工作。我坚信这与下面的我的本机代码有关。
此外,“ Chrome浏览器无法正常运行”的日志也未在Google Chrome浏览器中打印。
-
import React from 'react';
import { StyleSheet, Platform, Image, Text, View, ScrollView } from 'react-native';
import { GoogleSignin, statusCodes, GoogleSigninButton } from 'react-native-google-signin';
import NavigationManager from './containers/TabNavigator';
import PhoneStorageManager from './assets/jwtStorageManager';
GoogleSignin.configure({
forceConsentPrompt: true,
webClientId: '<client-id>
});
export default class App extends React.Component {
constructor() {
super();
this.state = {failed: "NOOO", user: null, jwt: null};
}
async componentDidMount() {
}
signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
this.setState({ failed: "SUCCESS" });
this.setState({ user: userInfo });
const resp = await fetch('<url>', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
session: {
token: userInfo.idToken,
authprovider: "google"
}
})
})
.then(response => response.json())
this.setState({ jwt: resp.jwt }, ()=> {
console.log(this.state.jwt, 'jwt');
PhoneStorageManager._storeData("key",this.state.jwt);
//so that i can use jwt for further api calls
});
console.log("this is not working");
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
this.setState({ failed: "CANCELLED" });
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (f.e. sign in) is in progress already
this.setState({ failed: "PROGRESS" });
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
// play services not available or outdated
this.setState({ failed: "NO AVAIL" });
} else {
// some other error happened
this.setState({error: error});
this.setState({ failed: "some other error happened" });
}
}
}
render() {
if (this.state.jwt) {
console.log(this.state.jwt);
return (
<NavigationManager JWT={this.state.jwt}/>
);
}
else
return (
<View style={styles.container}>
<Image source={require('./assets/auditlogo.png')} style={[styles.logo]} />
<GoogleSigninButton
style={{ width: 192, height: 48 }}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Dark}
onPress={this.signIn}
disabled={this.state.isSigninInProgress} />
<Text style= {styles.instructions}>
Please Sign in to continue.
</Text>
</View>
);
}
}
1)用户按下google登录按钮 2)提取api以创建用户(如果有新用户)。 3)提取api以创建会话。 (这将返回jwt以进行进一步的api调用) 4)使用jwt的其他api调用。 (我如何将jwt传递下来?)