Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): add signInWithCustomToken method #577

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
Expand Down Expand Up @@ -233,6 +234,51 @@ func (c *baseClient) CustomTokenWithClaims(ctx context.Context, uid string, devC
return info.Token(ctx, c.signer)
}

// SignInWithCustomTokenResponse represents a result of exchanging a custom Auth token.
type SignInWithCustomTokenResponse struct {
// An Identity Platform ID token for the authenticated user.
IdToken string `json:"idToken"`

// An Identity Platform refresh token for the authenticated user.
RefreshToken string `json:"refreshToken"`

// The number of seconds until the ID token expires.
ExpiresIn string `json:"expiresIn"`

// Whether the authenticated user was created by this request.
IsNewUser bool `json:"isNewUser"`
}

// SignInWithCustomToken signs in or signs up a user by exchanging a custom Auth token.
// Upon a successful sign-in or sign-up, a new Identity Platform ID token and refresh token are
// issued for the user.
func (c *baseClient) SignInWithCustomToken(ctx context.Context, token string, tenantId string) (resp *SignInWithCustomTokenResponse, err error) {
if token == "" {
err = errors.New("custom token must not be empty")
return
}

payload := map[string]interface{}{
"token": token,
"returnSecureToken": true,
}
if tenantId != "" {
payload["tenantId"] = tenantId
}

resp = &SignInWithCustomTokenResponse{}
req := &internal.Request{
Method: http.MethodPost,
URL: fmt.Sprintf("%s/accounts:signInWithCustomToken", c.userManagementEndpoint),
Body: internal.NewJSONEntity(payload),
}
if _, err = c.httpClient.DoAndUnmarshal(ctx, req, resp); err != nil {
resp = nil
return
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the return statement here is not necessary.

}
return
}

// SessionCookie creates a new Firebase session cookie from the given ID token and expiry
// duration. The returned JWT can be set as a server-side session cookie with a custom cookie
// policy. Expiry duration must be at least 5 minutes but may not exceed 14 days.
Expand Down