0
votes

i am trying create user with custom claim. I am using Firebase Cloud Functions. The problem is, when i create (Sign Up) an user, the onCreate not trigger. I am following this tutorial of provided by google. https://firebase.google.com/docs/auth/admin/custom-claims I Deployed my functions and the region is us-central1

Cloud functions version :

 firebase-admin": "^8.9.0
 firebase-functions": "^3.3.0

I am using Vue JS as Front-end

My functions/Index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.ProccessSignUp = functions.auth.user().onCreate(async (user) =>{
   console.log("Email"+user.email);

   if (user.email){

        const customClaims = {
            admin:true
        };

        return admin.auth().setCustomUserClaims(user.uid,customClaims)
            .then(() =>{
                const metadataRef = admin.database().ref('metadata/' +user.uid);
                return metadataRef.set({refeshTime:new Date().getTime()})
            }).catch(err =>{
                console.log(err.message)
            })
    }



});

My SignUpWithEmailAndPassword

userSignUp({dispatch},payload){
            firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password)
                .then(user =>{
                        user.user.sendEmailVerification()
                            .then(() =>
                                alert('Your account has been created! Please, verify your account'),
                                dispatch('userSignOut'),
                            ).catch(err =>{
                                console.log(err.message)
                        })
                }).catch(err =>{
                     console.log(err.message)
            })
        },

oAuthStateChanged

router.beforeEach(async (to, from, next) => {
    const user = await new Promise((resolve) => {
        firebase.auth().onAuthStateChanged(async user => {
            await store.dispatch("autoSignIn", user);
            resolve(user)
        });
    });

    const requiresAuth = to.matched.some(record => record.meta.requiresAuth)



    if (requiresAuth) {
        if (!user){
            next(false)
        }else {
            if (user.emailVerified){

                next();
            }else {

                alert('Please verify your account')
                await store.dispatch("userSignOut", user);
            }
        }

    } else {

        next()

    }
});


1
How do you detect that the onCreate Cloud Function is not triggered? Do you see anything in the Firebase console? Have you checked if the user has the claim? - Renaud Tarnec
@Reanud Tarnec I detect, when i create a new user, it's doesn't save in the database. - mamun mohamed
What do you mean by "it's doesn't save in the database"? The user is not saved in the Auth database? - Renaud Tarnec
If you add a console.log in userSignUp like firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password).then(user =>{ console.log('User Created'); ....}); do you see this console output in your browser console? - Renaud Tarnec
Yes, I see the output in browser and when say it's not saving in database, I meant if you look at my funtions/index.js I said onCreate new user save the user credentials in the database. - mamun mohamed

1 Answers

1
votes

As explained in the doc, with Cloud Functions you can "emit a log line from your function, use standard JavaScript logging calls such as console.log and console.error".

Then the Cloud Functions logs are viewable either in the Firebase console, Stackdriver Logging UI, or via the firebase command-line tool.

So you should be able to confirm that your Cloud Function runs correctly (or not) by looking at, for exemple, the Firebase console.