Search This Blog

Monday, March 6, 2017

Firebase create user with email, password, display name and photo url


Problem

In Firebase you can create user account with email and password. But what if you want to set user displayName and photo?  For that purpose after creating user with email and password you can use FIRUserProfileChangeRequest.

Algorithm


  1. Create new user with email and password with completion handler with method FIRAuth.auth()?.createUser
  2. In success of completion handler use method of user  user.profileChangeRequest() and set user display name changeRequest.displayName and commit changes changeRequest.commitChanges

Source Code

     /* 
     1. Create new user account with email and password.
     2. Update create account and set user display name.
     */
    func registerNewUser(email: String,
                  password: String,
                  displayName: String,
                  successHandler: ((_ uid:String) -> Void)?,
                  errorHandler: ((_ errorString: String) -> Void)?) {
        FIRAuth.auth()?.createUser(
            withEmail: email,
            password: password,
            completion: { (user, error) in
                if let error = error {
                    if let errorHandler = errorHandler {
                        errorHandler(error.localizedDescription)
                    }
                } else if let user = user {
                    let changeRequest = user.profileChangeRequest()
                    changeRequest.displayName = displayName
                    changeRequest.commitChanges(completion: { error in
                        if let error = error {
                            if let errorHandler = errorHandler {
                                errorHandler(error.localizedDescription)
                            }
                        } else if let successHandler = successHandler {
                            successHandler(user.uid)
                        }
                    })
                }
        })
    }

Link to Firebase Documentation



6 comments:

  1. Thanks for sharing such useful information. Thanks You I Read Your article its really informative. Keep up the good work. I hope to see more interesting articles from you.
    -Custom Website Design Company

    ReplyDelete
  2. After a long time i found a unique and on purpose information about Custom Designed Websites. Can't wait to have more from you.

    ReplyDelete
  3. You've written a fascinating piece of writing. This is exactly the kind of information I was looking for. Please provide me with more relevant information so that I can learn more. Best Custom Websites

    ReplyDelete
  4. Great content. I was looking for this kind of content. It saved my time to search further.
    You must provide such type of content constantly. I appreciate your willingness to provide readers with a good article.
    Custom Design Websites

    ReplyDelete
  5. Thank you very much for drawing my notice to this. Your blog is jam-packed with useful facts. Until I read that, I would have no idea. I'll come back for more fantastic material.
    Best wishes, and good luck.
    Web Design USA

    ReplyDelete
  6. Good content. I was looking for this type of content. Saved my time for further search.
    You should always provide that type of content. I appreciate your determination to give the students a good title.Custom Designed Websites

    ReplyDelete