ReactNative进行匿名身份验证时出现“无法使活动用户成为注销用户”的错误-Stitch-MongoDB

时间:2019-05-04 19:08:46

标签: mongodb react-native authentication

我正在尝试使用MongoDB(使用模块“ mongodb-stitch-react-native-sdk”)对React Native应用程序进行匿名身份验证。最初,匿名身份验证工作正常,但过了一会儿我收到了此错误"Failed to log in anonymously: n: (UserNotLoggedIn): cannot make the active user a logged out user; please use loginWithCredential() to switch to this user"

环顾了https://stitch.mongodb.com上的Stitch应用设置,尝试删除所有用户。在我看来,该错误听起来像是尝试建立连接到已注销的现有用户,而不是尝试创建新的匿名用户(这是我认为匿名登录所做的事情)。完全相同的代码适用于我的项目合作伙伴,因此我想它与IP /缓存有关,但我想了解为什么会这样。或可能完全是另外一回事-这是我们的第一个React Native应用程序,也是我们第一次使用MongoDB,因此我们一点也不经验丰富。

他正在使用Expo应用程序的IOS版本,而我正在使用Android版本的Android(如果有任何关系)。我们俩都使用“隧道”连接(这是唯一对我们俩都起作用的连接)。

相关代码如下:

import React from 'react'
import { Button, StyleSheet, Text, View } from 'react-native';
import { Stitch, AnonymousCredential } from 'mongodb-stitch-react-native-sdk';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      currentUserId: undefined,
      client: undefined
    };
    this._loadClient = this._loadClient.bind(this);
    this._onPressLogin = this._onPressLogin.bind(this);
    this._onPressLogout = this._onPressLogout.bind(this);
    this._onPressRegister = this._onPressRegister.bind(this);
  }

  componentDidMount() {
    this._loadClient();
  }

  render() {
    let loginStatus = "Currently logged out."

    if(this.state.currentUserId) {
      loginStatus = `Currently logged in as ${this.state.currentUserId}!`
    }

    loginButton = <Button
                    onPress={this._onPressLogin}
                    title="Login"/>

    logoutButton = <Button
                    onPress={this._onPressLogout}
                    title="Logout"/>

    return (
      <View style={styles.container}>
        <Text> {loginStatus} </Text>
        {this.state.currentUserId !== undefined ? logoutButton : loginButton}
        {registerButton}
      </View>
    );
  }

  _loadClient() {
    Stitch.initializeDefaultAppClient('hobbyproject-shcheduler-blijw').then(client => {
      this.setState({ client });

      if(client.auth.isLoggedIn) {
        this.setState({ currentUserId: client.auth.user.id })
      }
    });
  }

  _onPressLogin() {
    this.state.client.auth.loginWithCredential(new AnonymousCredential()).then(user => {
        console.log(`Successfully logged in as user ${user.id}`);
        this.setState({ currentUserId: user.id })
    }).catch(err => {
        console.log(`Failed to log in anonymously: ${err}`);
        this.setState({ currentUserId: undefined })
    });
  }

  _onPressLogout() {
    this.state.client.auth.logout().then(user => {
        console.log(`Successfully logged out`);
        this.setState({ currentUserId: undefined })
    }).catch(err => {
        console.log(`Failed to log out: ${err}`);
        this.setState({ currentUserId: undefined })
    });
  }
}

这是本教程中的大部分代码:https://docs.mongodb.com/stitch/authentication/anonymous/

我们将非常感谢您的帮助,我们都检查了该错误,但我们无法解决。当然,任何可能在Google搜索上很难找到但有用的资源也将受到赞赏。

0 个答案:

没有答案