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

Added a method to login with email and password with was missing in t… #630

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions auth/email_action_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)

// ActionCodeSettings specifies the required continue/state URL with optional Android and iOS settings. Used when
Expand Down Expand Up @@ -134,3 +136,67 @@ func (c *baseClient) generateEmailActionLink(
_, err := c.post(ctx, "/accounts:sendOobCode", payload, &result)
return result.OOBLink, err
}


//LoginRequest represents the the request payload
type LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
ReturnSecureToken bool `json:"returnSecureToken"`
}

//response
type LoginResponse struct {
IDToken string `json:"idToken"`
RefreshToken string `json:"refreshToken"`
ExpiresIn string `json:"expiresIn"`
Email string `json:"email"`
}


// LoginWithEmailAndPassword logs in a user using email and password with structured logging and detailed error handling.
func (c *baseClient) LoginWithEmailAndPassword(ctx context.Context, email, password string) (*LoginResponse, error) {
if email == "" || password == "" {
return nil, errors.New("email and password must not be empty")
}

payload := LoginRequest{
Email: email,
Password: password,
ReturnSecureToken: true,
}

_, err := c.GetUserByEmail(ctx, email)
if err != nil {
return nil, fmt.Errorf("no account associated with this email")
}

b, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %w", err)
}

req, err := http.NewRequestWithContext(ctx, "POST", c.userManagementEndpoint+"/accounts:signInWithPassword", strings.NewReader(string(b)))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Content-Type", "application/json")

res, err := c.httpClient.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("request to authentication service failed: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid email or password: status code %d", res.StatusCode)
}

var result LoginResponse
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return &result, nil
}
2 changes: 2 additions & 0 deletions auth/user_mgt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,8 @@ func (c *baseClient) CreateUser(ctx context.Context, user *UserToCreate) (*UserR
return c.GetUser(ctx, uid)
}



func (c *baseClient) createUser(ctx context.Context, user *UserToCreate) (string, error) {
if user == nil {
user = &UserToCreate{}
Expand Down