Ajay Singh Beniwal
Ajay Singh Beniwal

Reputation: 19037

How to get Firebase JWT token to validate on server side in Latest Firebase IOS SDK

I am trying out new Firebase IOS SDK with firebase base authentication and my custom backend server . I have enabled username password authentication in firebase which works fine but I do not see any property on auth object which gives me the JWT token so that I can verify the requests on my custom backend server

FIRAuth.auth()?.createUserWithEmail(singupusername.text!, password: singuppassword.text!) { (user, error) in
            print(user)
            print(error)
        }

the user does not have any property which gives me the firebase JWT token

Upvotes: 2

Views: 1475

Answers (2)

XYD
XYD

Reputation: 683

the latest code should be

let currentUser = Auth.auth().currentUser
currentUser?.getIDTokenForcingRefresh(true) { idToken, error in
    if let error = error {
        // Handle error
        return;
    }

    // Send token to your backend via HTTPS
    // ...
    print("JWT is< \(idToken)>")
            
}

Upvotes: 1

Ajay Singh Beniwal
Ajay Singh Beniwal

Reputation: 19037

I figured out method to get access token in the latest SDK I used below method to get the latest token

 FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let user = user{
                user.getTokenWithCompletion{
                    (data) in
                    print(data.0)
                }
            }
        }

Upvotes: 2

Related Questions