在 Expo 中使用 Firebase 验证 Google 提供的 ID 令牌

Verify ID Token provided by Google with Firebase in Expo

我最近允许用户使用 Google 0Auth2.0 登录我的 Expo 应用。

问题是当我需要验证后端提供的令牌时,出现 400 错误。

(仅使用 Google 提供的令牌,而不使用我从基本 email/password 身份验证中获得的令牌)

export async function getUser(id){
    console.log("Reading user... ")         
    try{
        const token = await auth.currentUser.getIdToken()
        const response = await axios.get(`${root}/users/get_currentuser/${id}?auth=${token}`)
        return response.data
    }
    catch(err){
        console.log(err.message)
    }
}
const get_currentuser = async (req, res) => {

    const id = req.params.id
    const auth = req.query.auth
    try {
        const result = await getAuth().verifyIdToken(auth) // the error is here 
    
        if(result.uid === id){
            try {
                const user = await db.collection("Users").findOne({uid : id})
                res.status(200).json(user)


            } catch (error) {
                res.status(500).json(error)
            }
        }
        else
            res.status(400).json({err : "Connexion impossible"})

    } catch (error) {
        res.status(400).json({err : "Connexion impossible / token error "})
    }
    
}

在此先感谢您的帮助!

好的!我找到了解决方案。

我只记得我的 Firebase-Admin-SDK 不正确,因为我创建了一个新的 Firebase 项目,所以我使用了错误的私钥(我们在配置文件设置中生成的密钥)

所以为了解决这个问题,我生成了一个新的私钥,这对我很有帮助。