0
votes

So far everything has been working well but after adding firebase user auth to my app, my application starts misbehaving..

Here's my auth.js code

const { admin, db } = require('./admin');

module.exports = (request, response, next) => {
let idToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjIzNzA1ZmNmY2NjMTg4Njg2ZjhhZjkyYWJiZjAxYzRmMjZiZDVlODMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vdG9kby0zNjVlYSIsImF1ZCI6InRvZG8tMzY1ZWEiLCJhdXRoX3RpbWUiOjE2MDIzNDQyNDUsInVzZXJfaWQiOiJIeGRNcklxeVhQYlpaaWxEQ0NQaW9iNm15WngyIiwic3ViIjoiSHhkTXJJcXlYUGJaWmlsRENDUGlvYjZteVp4MiIsImlhdCI6MTYwMjM0NDI0NSwiZXhwIjoxNjAyMzQ3ODQ1LCJlbWFpbCI6InNhbXVlbG9ub2phOTcwQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJzYW11ZWxvbm9qYTk3MEBnbWFpbC5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.mOmV_V69IfCbQuf_QBiNgCsXCwlU4OTkT8fDVp6lkxtaBM8ZfOG55ymRikKJZ6J7z4siyT4sV89p4Ke0LFTX3KcmtkWNVQ0ERpWBvIi8hK2aqO53HbfUUo278tCGZAChx-xC_Drphz7xg8VJAqUm-rxBumqOBSukgivLXJLkBcvW87cNI2JPBmQr_DCL1FVWTYq9cQ9clVoSJDvbAaGZfay9nH5fuLXNs7m0CnFasqRerUoJ41Ro8kelfBMrzsXMp9bwevJNSrgBkjTyN7x5IURdVD8Ln4A6rIIxEJG-xmCmKHluFv3z_hlMEeFDGopQjGTJjeoYWtvh1gmRBnEcjw";
admin
    .auth()
    .verifyIdToken(idToken)
    .then((decodedToken) => {
        request.user = decodedToken;
        return db.collection('users').where('userId', '==', 
request.user.uid).limit(1).get();
    })
    .then((data) => {
        request.user.username = data.docs[0].data().username;
        request.user.imageUrl = data.docs[0].data().imageUrl;
        return next();
    })
    .catch((err) => {
        console.error('Error while verifying token', err);
        return response.status(403).json(err);
    });
};

Got the token id from one of the account i added to firebase but i keep getting error on console: "> Error while verifying token TypeError: Cannot read property 'data' of undefined"

also whenever i try to send GET request, i get only {} printed out

2

2 Answers

0
votes

The error is suggesting that your query returned no documents. You should check to see if the data (a QuerySnapshot object) contains any documents at all before trying to access them in the docs array.

I'll rename it to qs and show a more idiomatic way to use it:

    .then((qs) => {
        if (!qs.empty) {
            const data = qs.docs[0].data()
            request.user.username = data.username;
            request.user.imageUrl = data.imageUrl;
            return next();
        }
        else {
            // decide what you want to do if there are no documents
        }
    })
0
votes

Thank ! I got it working.

module.exports = (request, response, next) => {
let idToken;
if (request.headers.authorization && request.headers.authorization.startsWith('Bearer ')) {
    idToken = request.headers.authorization.split('Bearer ')[1];
} else {
    console.error('No token found');
    return response.status(403).json({ error: 'Unauthorized' });
}
// idToken comes from the client app
admin
admin
    .auth()
    .verifyIdToken(idToken)
    .then((decodedToken) => {
        request.user = decodedToken;
        return Promise.resolve(db.collection('users').limit(1).get());
    })
    .then((data) => {
        console.log(data)
        request.user.username = data.docs[0].data().username;
        request.user.imageUrl = data.docs[0].data().imageUrl;
        return next();
    })
    .catch((err) => {
        console.error('Error while verifying token', err);
        return response.status(403).json(err);
    });
};