Skip to content

FBAuth function not working #22

@xbox224

Description

@xbox224

I am trying to make this app. I am working on FBAuth function, but it doesn't work. Postman shows me {} as a response, and firebase logs shows:

Error while verifying token  TypeError: Cannot read property 'data' of undefined
    at FBAuth.admin.auth.verifyIdToken.then.then (/srv/index.js:71:46)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7) 

My code looks like this:

const FBAuth = (req, res, next) => {
    let idToken;
    if (
      req.headers.authorization &&
      req.headers.authorization.startsWith('Bearer ')
    ) {
      idToken = req.headers.authorization.split('Bearer ')[1];
    } else {
      console.error('No token found');
      return res.status(403).json({ error: 'Unauthorized' });
    }
  
    admin.auth().verifyIdToken(idToken)
        .then((decodedToken) => {
            req.user = decodedToken;
            return db
              .collection('users')
              .where('userId', '==', req.user.uid)
              .limit(1)
              .get();
          }).then((data) => {
            req.user.username = data.docs[0].data().username;
            return next();
          }).catch((err) => {
            console.error('Error while verifying token ', err);
            return res.status(403).json(err);
          });
};

Can anybody help me?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions