The function tries to retrieve all the users and then loops through each one and finds the ones that are premium and saves it to an array. For some reason the array is returned empty but the console log displays the correct value, i think the prblem might be that the auth user get is a async function but I couldnt figure that out.
Function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.logPremiumUsers = functions.https.onRequest((req, res) => {
const usersRef = admin.firestore().collection('users');
const query = usersRef //.where('time', '>=', 100).where('time', '<=', 1000);
const premiumUsers = [];
query.get()
.then((snapshot) => {
snapshot.forEach(async (doc) => {
const uid = doc.data().uid;
console.log(uid)
const userRecord = await admin.auth().getUser(uid)
const userData = userRecord.toJSON();
if (userData.customClaims.stripeRole === 'premiun') {
console.log(userData)
premiumUsers.push(userData);
}
});
res.status(200).json(premiumUsers);
})
.catch((error) => {
console.error(error);
res.status(500).send('Error getting premium users');
});
});
The console output:
i functions: Beginning execution of "us-central1-logPremiumUsers"
⚠ Google API requested!
- URL: "https://oauth2.googleapis.com/token"
- Be careful, this may be a production service.
> UKjZeASTvYeYm7cA6HvXc5JxqXn1
i functions: Finished "us-central1-logPremiumUsers" in 1459.693751ms
> {
> uid: 'UKjZeASTvYeYm7cA6HvXc5JxqXn1',
> email: 'a.kiselev.private@gmail.com',
> emailVerified: false,
> displayName: 'andrey',
> photoURL: undefined,
> phoneNumber: undefined,
> disabled: false,
> metadata: {
> lastSignInTime: 'Wed, 15 Feb 2023 15:35:21 GMT',
> creationTime: 'Tue, 14 Feb 2023 21:05:06 GMT',
> lastRefreshTime: 'Thu, 16 Feb 2023 08:38:04 GMT'
> },
> passwordHash: undefined,
> passwordSalt: undefined,
> customClaims: { stripeRole: 'premiun' },
> tokensValidAfterTime: 'Tue, 14 Feb 2023 21:05:06 GMT',
> tenantId: undefined,
> providerData: [
> {
> uid: 'a.kiselev.private@gmail.com',
> displayName: 'andrey',
> email: 'a.kiselev.private@gmail.com',
> photoURL: undefined,
> providerId: 'password',
> phoneNumber: undefined
> }
> ]
> }