Reputation: 65
I am creating a RESTful API using the firebase admin SDK, entirely in Golang.
The problem occurs when attempting to integrate token-based authentication, which requires both client SDK and admin SDK
For example,
firebase client SDK provides methods such as signInWithCustomToken
and signInWithEmailAndPassword
which outputs ID tokens to be handled with methods in Admin SDK.
firebase admin SDK provides methods such as VerifyIDToken
to verify token passed from the client.
In the latest firebase documentation, it says,
The Firebase Admin SDKs bundle the Google Cloud client libraries for Cloud Firestore alongside client libraries and SDKs for several other Firebase features.
Since I needed to use both Client and Admin SDK with Golang, I have decided to use the Admin SDK.
However, Admin SDK documentation in Golang does not have methods such as signInWithCustomToken
and signInWithEmailAndPassword
.
If I were to build a RESTful API with Golang, do I have to use another front-end programming languages like JavaScript to achieve token-based authentication?
In JavaScript Admin SDK, they DO have methods available such as signInWithCustomToken
and signInWithEmailAndPassword
.
I would like to know the workaround to write token-based authentication when necessary methods are not documented with Golang in Admin SDK.
Upvotes: 1
Views: 4337
Reputation: 14699
In addition to the answer of @Doug Stevenson, you could use the code in auth_test.go as an example:
func signInWithCustomToken(token string) (string, error) {
req, err := json.Marshal(map[string]interface{}{
"token": token,
"returnSecureToken": true,
})
if err != nil {
return "", err
}
apiKey, err := internal.APIKey()
if err != nil {
return "", err
}
resp, err := postRequest(fmt.Sprintf(verifyCustomTokenURL, apiKey), req)
if err != nil {
return "", err
}
var respBody struct {
IDToken string `json:"idToken"`
}
if err := json.Unmarshal(resp, &respBody); err != nil {
return "", err
}
return respBody.IDToken, err
Upvotes: 3
Reputation: 317497
The Admin SDK is provides functionality for use with backend code. With Firebase Auth, users are intended to sign in on the frontend and pass an ID token to the backend. If you are suggesting that you would like to use golang to write a frontend-like app, you won't find any APIs for that. You can call the Firebase Auth REST APIs directly in that case.
Upvotes: 2