Verify firebase id token Issue

Viewed 522

When I run the code , it says -

###
ReferenceError: getAuth is not defined <br/>
    at E:\Web Development\burj-al-arab-server\index.js:53:1
    at Layer.handle [as handle_request] (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\layer.js:95:5)
    at E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\index.js:335:12)
    at next (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\index.js:275:10)
    at jsonParser (E:\Web Development\burj-al-arab-server\node_modules\body-parser\lib\types\json.js:110:7)
    at Layer.handle [as handle_request] (E:\Web Development\burj-al-arab-server\node_modules\express\lib\router\layer.js:95:5)
###

added these code on server side. **Node.js **


app.get('/allbookings',(req, res) =>{
    console.log(req.query.authorization)

// idToken comes from the client app
getAuth()
  .verifyIdToken(idToken)
  .then((decodedToken) => {
    const uid = decodedToken.uid;
  
    // ...
  })
  .catch((error) => {
    // Handle error
  });

     collection.find({email : req.query.email})
     .toArray((err, documents) =>{
      res.send(documents)
    })
})
1 Answers

It seems you are using Firebase Client SDK on server side but the verifyIdToken() method exists in Firebase Admin SDK. First install the Admin SDK by npm install firebase-admin and then try the following:

const admin = require("firebase-admin")

admin.initializeApp()

app.get('/allbookings', (req, res) => {
  console.log(req.query.authorization)

  // idToken comes from the client app
  admin.auth().verifyIdToken(idToken).then((decodedToken) => {
    const uid = decodedToken.uid;
  
    // ...
  })
  .catch((error) => {
    // Handle error
  });
})

If you are not using Cloud functions and running this Express server on your server then you might need a service account to initialize the Admin SDK.

Related