React Native auth.verifyIdToken的Firebase问题

b5buobof  于 2023-05-18  发布在  React
关注(0)|答案(2)|浏览(172)

我正在构建一个react-native应用程序,现在我正在尝试使用我存储的JWT显示我的用户名,所以它在我的代码中看起来像这样:

import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'
import React, { useState } from 'react'
import firebase from '../../firebase';
import { auth } from '../../firebase'
import { signOut } from 'firebase/auth'
import { useEffect } from 'react';
import { useNavigation } from '@react-navigation/native'
import * as SecureStore from 'expo-secure-store';
import { globalStyles } from '../../styles/global'


const SearchScreen = () => {

    const [user, setUser] = useState(null);
    

    // Get the current user's ID token
    const getCurrentUser = async (key) => {
      const idToken = await SecureStore.getItemAsync(key);
      // Decode the ID token to get user information
      const decodedToken = await auth.verifyIdToken(idToken);
      console.log("here is the decoded one :",decodedToken)
      // Set the user's information to the state
      setUser(decodedToken);
    };

    getCurrentUser('jwt');

我确实在根目录下的“firebase.js”文件中设置了我的firebase:

import { getAuth } from "firebase/auth";
import firebase from 'firebase/compat';

const firebaseConfig = {
// private infos & shit you know
};

let app;
if (firebase.apps.length === 0) {
    app = firebase.initializeApp(firebaseConfig);
} else {
    app = firebase.app()
}

const auth = getAuth();

export { auth };

所以在这里我试图弄清楚为什么我会得到这个错误:
WARN可能未处理的Promise拒绝(id:1):TypeError:undefined不是函数

rkkpypqq

rkkpypqq1#

verifyIdToken不是React Native库为Firebase身份验证提供的方法。在API documentation中,你可以看到它没有列出任何地方。
验证ID令牌的功能仅保留给Firebase Admin后端SDK,该SDK具有需要验证调用代码的用户的代码。您可以阅读有关here的文档。Firebase Admin不适用于前端应用程序。

b4lqfgs4

b4lqfgs42#

您可以使用Firebase REST API验证您的ID令牌。查看API文档的这一部分
PS:这将只验证firebase的ID令牌。如果您使用的是自定义JWT令牌,这将不起作用

相关问题