how to get bearer token from firebase on ios in swift

To get a firebase auth bearer token on iOS with Swift, you can use FirebaseAuth framework to sign in with user's credentials to get the token. Here is an example:

  1. First, import FirebaseAuth:
main.swift
import FirebaseAuth
20 chars
2 lines
  1. Then, sign in with user's credentials:
main.swift
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
    // check if there's an error
    guard let StrongSelf = self else {
        return
    }
    
    if let error = error {
        // handle the error
        print(error.localizedDescription)
        return
    }
    
    // get the token for current user
    authResult?.user.getIDToken(completion: { (idToken, error) in
        if let error = error {
            // handle the error
            print(error.localizedDescription)
            return
        }
        
        print("Id Token: \(idToken)")
    })
}
611 chars
24 lines
  1. In the above code snippet, we first sign in the user with the email and password. After successful authentication, we get the token associated with the user using getIDToken(completion:) function.

Note: This token can be used to authenticate Firebase Services by providing it in the Authorization header of the request.

related categories

gistlibby LogSnag