Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-oauth

Creating a new user via Firebase Cloud Functions - account created but can not sign in


I am new here.

I am currently developing an app using Vue.js with firebase auth, firebase realtime database and firebase cloud functions as a backend. One part of the functionalities that the app has to include an admin account that has a possibility to create an accounts for other people. After creation new user receives an email with a login and a password to log in.

Because the signing up method (https://firebase.google.com/docs/auth/web/password-auth) automatically relog user to a newly created account which is obviously not wanted behavior I found a way to create a user via cloud functions. The code successfully creates an account in the firebase authentication panel with the exception that I cannot log in to the newly created accounts. I get a message that: "The password is invalid or the user does not have a password".

Additionally I am not sure if it means anything in this case but the accounts created with the cloud functions method does not have a mail icon in firebase authentication panel (Image).

Cloud Function Code:

exports.createUser = functions.https.onCall((data, context) => {
console.log(data)
return admin.auth().createUser({
    email: data.email,
    emailVerified: true,
    password: data.password,
    displayName: data.email,
    disabled: false,
  })
    .then(user => {
        return {
            response: user
      }
    })
    .catch(error => {
        throw new functions.https.HttpsError('failed to create a user')
    });

});

Sign In Code:

signIn: async function(){
  if(this.email && this.password){

    let getUsers = firebase.functions().httpsCallable('getUsers')

    this.feedback = null
    this.spin = true

    let destination = null
    let logedUser = null
    let type = null

    this.feedback = 'Logging in...'

    await firebase.auth().signInWithEmailAndPassword(this.email, this.password)
      .then(response => {
        this.feedback = 'Authorization finished...'
        logedUser = response.user
      })
      .catch( error => {
        this.feedback = error.message
        this.spin = false
      })
//... more code here but I am certain it has nothing to do with the problem.

Solution

  • Due to the asynchronous character of the HTTPS callable function, with your current code you are trying to sign-in before the user is completely created through the Cloud Function.

    In addition, you are actually not calling the Cloud Function with the mail and password parameters.

    You should do as follows, based on the documentation.

    ....
    let getUsers = firebase.functions().httpsCallable('getUsers')
    
    await getUsers({email: this.email, password: this.password})
      .then(result => {
          return firebase.auth().signInWithEmailAndPassword(this.email, this.password)
      })
      .then(userCredential => {
        this.feedback = 'Authorization finished...'
        logedUser = userCredential.user
        return true
      })
      .catch( error => {
        this.feedback = error.message
        this.spin = false
      })
    ....