简体   繁体   English

MongoDB Stitch:linkWithCredential 提供无效的用户名/密码

[英]MongoDB Stitch: linkWithCredential gives invalid username/password

I am making a React Native app.我正在制作一个 React Native 应用程序。 I have been implementing Anonymous Login when user enters the app.当用户进入应用程序时,我一直在实施匿名登录。 After certain action, user can signup.经过一定的操作,用户可以注册。 When I try to link the current anonymous user with email and password, it says [StitchServiceError: invalid username/password].当我尝试将当前匿名用户与 email 和密码链接时,它显示 [StitchServiceError: invalid username/password]。 I don't know why.我不知道为什么。 I have been following the exact same documentation to linkWithCredential.我一直在遵循与 linkWithCredential 完全相同的文档。

Here is my code:这是我的代码:

const auth = Stitch.defaultAppClient.auth;
const collection = Stitch.defaultAppClient
      .getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')
      .db('snack')
      .collection('users');
const {email, password, ...rest} = currentUser;
return from(
      auth.user.linkWithCredential(
        new UserPasswordCredential(email, password),
      ),
    ).pipe(switchMap(() => .....))

I am using Redux Observable for side effects.我正在使用 Redux 可观察到副作用。

I have enabled Email/Password and Anonymous Authentication in the settings.我在设置中启用了电子邮件/密码和匿名身份验证。 I don't know what is wrong here and why is it giving me username and password error when I am trying to signup and link the user.我不知道这里出了什么问题,为什么在我尝试注册和链接用户时给我用户名和密码错误。

Ah, silly.啊,傻。 I am coming from Firebase background.我来自 Firebase 背景。 So thought it would work the same way.所以认为它会以同样的方式工作。 But it doesn't.但事实并非如此。

So, I have to create an account first.所以,我必须先创建一个帐户。 And then link it.然后链接它。 In Firebase.在 Firebase。 you can directly link the account and they create a new account and link it with your anonymous session automatically.您可以直接链接该帐户,他们会创建一个新帐户并自动将其与您的匿名 session 链接。

Here is the updated code:这是更新的代码:

const {email, password, ...rest} = currentUser;

const emailPasswordClient = Stitch.defaultAppClient.auth.getProviderClient(
      UserPasswordAuthProviderClient.factory,
    );

    return from(
      emailPasswordClient.registerWithEmail(email, password),
    ).pipe(
      switchMap(() => {
        return from(
          auth.user.linkWithCredential(
            new UserPasswordCredential(email, password),
          ),
        ).pipe(
          switchMap(() => ...)
.....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM