Skip to content

Commit

Permalink
add better http error messages in firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Sep 14, 2024
1 parent 6c6ee08 commit 3c8c110
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions internal/store/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
var App *firebase.App
var Ctx = context.Background()

//go:embed simple-ssh-manager-firebase-adminsdk-y7ei5-ac0913e54f.json
//go:embed simple-ssh-manager-firebase-adminsdk-y7ei5-f496dc420f.json
var firebaseConfig embed.FS

// InitFirebase initializes the Firebase app and assigns it to App
Expand Down Expand Up @@ -116,21 +116,27 @@ func authenticateWithFirebase(email, password string) (map[string]interface{}, e
}
}(resp.Body)

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("wrong Credentials: %d", resp.StatusCode)
}

var response map[string]interface{}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
errorMessage := "Unknown error"
if errMsg, ok := response["error"].(map[string]interface{}); ok {
if msg, ok := errMsg["message"].(string); ok {
errorMessage = msg
}
}
return nil, fmt.Errorf("authentication failed: %s (Status: %d)", errorMessage, resp.StatusCode)
}

idToken, ok := response["idToken"].(string)
if !ok {
return nil, fmt.Errorf("error extracting ID token from response")
Expand Down Expand Up @@ -190,8 +196,25 @@ func ResetPassword(email string) error {
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}

var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("error unmarshalling response: %v", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error response from server: %d", resp.StatusCode)
errorMessage := "Unknown error"
if errMsg, ok := response["error"].(map[string]interface{}); ok {
if msg, ok := errMsg["message"].(string); ok {
errorMessage = msg
}
}
return fmt.Errorf("password reset failed: %s (Status: %d)", errorMessage, resp.StatusCode)
}

fmt.Printf("Please check your %s inbox for password reset link.\n", email)
Expand Down

0 comments on commit 3c8c110

Please sign in to comment.