简体   繁体   中英

Verify ID Token provided by Google with Firebase in Expo

i recently allowed users to sign-in in my Expo app with Google 0Auth2.0.

The problem is that when i need to verify the token provided in my backend, i have a 400 error.

(only with Token provided by Google not with a token that i get from basic email/password authentification)

  • In my client:
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)
    }
}
  • In my backend:
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 "})
    }
    
}

Thanks in advance for any help !

Okey. I found the solution.

I just remember that my Firebase-Admin-SDK wasn't correct because i created a new Firebase project, so i used the wrong Private Key ( the key that we generate in Profil Setting )

So to fix the issue i generated a new Private Key and that helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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