使用云功能登录反应原生用户

时间:2021-01-02 23:59:46

标签: reactjs firebase react-native react-native-firebase

我有一个 React Native 应用程序,我正在使用 react native firebase 云函数来管理后端。我使用 functions().httpsCallable('registerUser') 发出一个 Post 请求,它向我的身份验证模拟器发送一个请求,该模拟器调用 functions.https.onCall() 来注册用户,然后我将所需的数据发送回应用程序。

我的问题:

我正在使用云函数模拟器(模拟器是一种开发工具,它模拟对 firebase 的调用,而无需运行 firebase billing )来实现我的大部分身份验证逻辑,这与使用客户端 sdk 的方法不同。我正在尝试使用 auth().signInWithEmailAndPassword(email, password) 与我创建的用户一起登录应用程序,但我不断收到以下错误

<块引用>

[auth/user-not-found] 没有与此对应的用户记录 标识符。该用户可能已被删除。

这个错误是有道理的,因为我的所有数据(身份验证、数据库、firestore、函数等)都在模拟器上存储/运行,而不是实际的 firebase。

我的问题

  1. 如果我最初使用云功能创建用户,我该如何登录?

我尝试了什么:

根据firebase auth api我 在我的登录云功能中发出帖子请求

<块引用>

https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=APIKEY

返回电子邮件和密码

   {
        "kind": "identitytoolkit#VerifyPasswordResponse",
        "localId": "hjgvcdsifdsifiudsfdsyyuggyu",
        "email": "test@test.com",
        "displayName": "",
        "idToken": "<idToken>",
        "registered": true,
        "refreshToken": "<refreshToken>",
        "expiresIn": "3600"
    }

  
  1. 我如何使用返回的信息让客户端知道用户已经/可以登录?

  2. 当我尝试登录用户时,如何确保客户端 firebase sdk 从模拟器返回 idToken、refreshToken、email、firstname 等,而不是带有 auth().signInWithEmailAndPassword(email, password) 的真实 firebase?< /strong>

  3. 由于我正在使用云功能创建具有 admin.auth().createUser({...userData}) 的用户,我如何在创建用户后立即登录并让客户端将其识别为已登录?< /p>

我尝试了什么:

在我的云函数中使用 admin.auth().createUser({...userData}) 创建用户后,我尝试使用 auth().onAuthStateChanged((user) => {}) 但它返回 null

任何帮助或指出正确的方向都会有所帮助,提前致谢

应用根目录

import functions from '@react-native-firebase/functions';

if (__DEV__) {

  functions().useFunctionsEmulator('http://localhost:5001');
}

模拟器

enter image description here

用户注册后的数据

{
       uid: 'ejBeGtxn57nscHjWDBv0DxuzXIjm',
       email: 'test@test.com',
       emailVerified: false,
        displayName: 'test user',
        photoURL: undefined,
       phoneNumber: undefined,
        disabled: false,
        metadata: UserMetadata {
          creationTime: 'Sat, 02 Jan 2021 21:34:24 GMT',
          lastSignInTime: 'Sat, 02 Jan 2021 21:34:24 GMT',
          lastRefreshTime: null
        },
        providerData: [
          UserInfo {
           uid: 'test@test.com',
            displayName: 'tes user',
            email: 'test@test.com',
            photoURL: undefined,
           providerId: 'password',
           phoneNumber: undefined
          }
        ],
        passwordHash: 'fakeHash:salt=fakeSaltu6ylonzBuvO4SwwQKxOA:password=password',
        passwordSalt: 'fakeSaltu6ylonzBuvO4SwwQKxOA',
        tokensValidAfterTime: 'Sat, 02 Jan 2021 21:34:24 GMT',
        tenantId: undefined
}

1 个答案:

答案 0 :(得分:0)

这很简单,我只需要使用身份验证模拟器。

import React, {useState, useEffect} from 'react';
import {View, Text} from 'react-native';
import auth from '@react-native-firebase/auth';
import functions from '@react-native-firebase/functions';



// Use a local emulator in development
if (__DEV__) {

  functions().useFunctionsEmulator('http://localhost:5001');
  auth().useEmulator('http://localhost:9099'); //<--- here was my solution
}