uzair
uzair

Reputation: 766

Firebase Authentication - Retrieve Custom Claims key for iOS

A custom claim luid has been added to the Firebase Authentication from the backend, I'm looking for a way to access this key from the front end for an iOS application.

First I need to check if the key exists and if it exists then get its value.

What have I tried? Everything under Auth.auth().currentUser

Attaching a picture of the decoded JWT data, which shows the key luid

enter image description here

Upvotes: 1

Views: 605

Answers (2)

uzair
uzair

Reputation: 766

This will display a Dictionary with all the keys available for the current user.

 Auth.auth().currentUser?.getIDTokenResult(completion: { (authresult, error) in
     print("CurrentUser Keys", authresult!.claims.keys)
})

This will return Bool value based on a particular key's availability

Auth.auth().currentUser?.getIDTokenResult(completion: { (authresult, error) in
     print("CurrentUser Keys", authresult!.claims.keys.contains("luid"))
})

Upvotes: 0

Dharmaraj
Dharmaraj

Reputation: 50850

You can check the custom claims this way:

user.getIDTokenResult(completion: { (result, error) in
  guard let luid = result?.claims?["luid"] as? NSNumber else {
    // luid absent
    return
  }
  //Check for value here and use if-else
})

Detailed explanation can be found in documentation

Upvotes: 2

Related Questions