Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Attributes to Firebase Auth

I have hunted through Firebase's docs and can't seem to find a way to add custom attributes to FIRAuth. I am migrating an app from Parse-Server and I know that I could set a user's username, email, and objectId. No I see that I have the option for email, displayName, and photoURL. I want to be able to add custom attributes like the user's name. For example, I can use:

let user = FIRAuth.auth()?.currentUser
            if let user = user {
                let changeRequest = user.profileChangeRequest()

                changeRequest.displayName = "Jane Q. User"
                changeRequest.photoURL =
                    NSURL(string: "https://example.com/jane-q-user/profile.jpg")
                changeRequest.setValue("Test1Name", forKey: "usersName")
                changeRequest.commitChangesWithCompletion { error in
                    if error != nil {

                        print("\(error!.code): \(error!.localizedDescription)")

                    } else {

                        print("User's Display Name: \(user.displayName!)")
                        print("User's Name: \(user.valueForKey("name"))")

                    }
                }
            }

When I run the code, I get an error that "usersName" is not key value compliant. Is this not the right code to use. I can't seem to find another way.

like image 283
Dan Levy Avatar asked Jun 15 '16 03:06

Dan Levy


People also ask

Does Firebase Auth use JWT?

Firebase gives you complete control over authentication by allowing you to authenticate users or devices using secure JSON Web Tokens (JWTs). You generate these tokens on your server, pass them back to a client device, and then use them to authenticate via the signInWithCustomToken() method.

What is custom authentication in Firebase?

You can integrate Firebase Authentication with a custom authentication system by modifying your authentication server to produce custom signed tokens when a user successfully signs in. Your app receives this token and uses it to authenticate with Firebase.

What does Firebase auth () currentUser return?

console. log(firebase. auth(). currentUser) // This returns null console.

What does onAuthStateChanged do?

The module provides a method called onAuthStateChanged which allows you to subscribe to the users current authentication state, and receive an event whenever that state changes.


2 Answers

You can't add custom attributes to Firebase Auth. Default attributes have been made available to facilitate access to user information, especially when using a provider (such as Facebook).

If you need to store more information about a user, use the Firebase realtime database. I recommend having a "Users" parent, that will hold all the User children. Also, have a userId key or an email key in order to identify the users and associate them with their respective accounts.

Hope this helps.

like image 80
Fred Dupray Avatar answered Sep 19 '22 18:09

Fred Dupray


While in most cases you cannot add custom information to a user, there are cases where you can.

If you are creating or modifying users using the Admin SDK, you may create custom claims. These custom claims can be used within your client by accessing attributes of the claims object.

Swift code from the Firebase documentation:

user.getIDTokenResult(completion: { (result, error) in
  guard let admin = result?.claims?["admin"] as? NSNumber else {
    // Show regular user UI.
    showRegularUI()
    return
  }
  if admin.boolValue {
    // Show admin UI.
    showAdminUI()
  } else {
    // Show regular user UI.
    showRegularUI()
  }
})

Node.js code for adding the claim:

// Set admin privilege on the user corresponding to uid.

admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});
like image 39
giraffesyo Avatar answered Sep 19 '22 18:09

giraffesyo