hidjou / classsed-react-firebase-functions

327 stars 180 forks source link

FBAuth function not working #22

Open xbox224 opened 4 years ago

xbox224 commented 4 years ago

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?

Austin-Beers commented 4 years ago

How far did you make it in the video before having this issue? Everything was working fine for me Until I added the addUserDetails endpoint

usamaster commented 3 years ago

For anyone encountering this problem:

I made a typo in de collection name, it's 'users' instead of 'user' in the FBAuth admin part. Hopes this helps ;)