Skip to content

Commit

Permalink
update auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sagostin committed Sep 20, 2024
1 parent 971ed3b commit 4a9c66d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ type Login struct {

// Login returns error on fail, nil on success
func (r *Login) Login(ip string, db *mongo.Database) (string, error) {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login"}
if r.Email == "" {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", Message: "invalid email address"}
ee.Message = "invalid email"
ee.Print()
return "", fmt.Errorf(ee.Message)
}

u := users.User{Email: r.Email}
user, err := u.FromEmail(db)
if err != nil {
return "", err
ee.Message = "invalid email"
ee.Print()
return "", fmt.Errorf(ee.Message)
}

err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(r.Password))
if err != nil {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", ObjectID: user.ID, Message: "invalid password"}
ee.Message = "invalid password"
ee.Message += " - connecting ip: " + ip
ee.Print()
return "", fmt.Errorf(ee.Message)
}
Expand All @@ -50,7 +54,7 @@ func (r *Login) Login(ip string, db *mongo.Database) (string, error) {

err = session.Create(db)
if err != nil {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", ObjectID: user.ID, Message: "unable to create session", Error: err}
ee.Message = "unable to create session"
ee.Print()
return "", fmt.Errorf(ee.Message)
}
Expand All @@ -67,9 +71,9 @@ func (r *Login) Login(ip string, db *mongo.Database) (string, error) {
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(os.Getenv("KEY")))
if err != nil {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", Message: "unable to generate session token", Error: err}
ee.Message = "unable to sign token string"
ee.Print()
return "", err
return "", fmt.Errorf(ee.Message)
}

out := map[string]any{
Expand All @@ -79,7 +83,7 @@ func (r *Login) Login(ip string, db *mongo.Database) (string, error) {

bytes, err := json.Marshal(out)
if err != nil {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", Message: "unable to marshal token"}
ee.Message = "unable to marshal"
ee.Print()
return "", fmt.Errorf(ee.Message)
}
Expand Down

0 comments on commit 4a9c66d

Please sign in to comment.