Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FBAuth function not working #22

Open
xbox224 opened this issue Jul 14, 2020 · 2 comments
Open

FBAuth function not working #22

xbox224 opened this issue Jul 14, 2020 · 2 comments

Comments

@xbox224
Copy link

xbox224 commented Jul 14, 2020

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
Copy link

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
Copy link

usamaster commented Nov 28, 2020

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 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants