Skip to content

Commit

Permalink
Changed the controller file name, added correct errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KostLinux committed Jun 2, 2024
1 parent eb3cee4 commit cffe317
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions controller/api/login.go → controller/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func Signin(c *gin.Context) {
return
}

user := model.CheckPasswordMatch(data.Username, data.Password)
if user.ID == 0 {
c.Render(http.StatusUnauthorized, render.Data{})
user, err := model.CheckPasswordMatch(data.Username, data.Password)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}

err := helper.SetSession(c, user.ID)
err = helper.SetSession(c, user.ID)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ func CreateUser(email, username, password string) *User {
return &user
}

func CheckPasswordMatch(username, password string) *User {
func CheckPasswordMatch(username, password string) (*User, error) {
var user User
DB.Where("username = ?", username).First(&user)
if user.ID == 0 {
return &user
return nil, errors.New("invalid username")
}

err := helper.CheckPasswordMatch(user.Password, password)
if err != nil {
return &User{}
return nil, errors.New("invalid password")
}
return &user
return &user, nil
}

func GetUserByID(userID uint) (User, error) {
Expand Down

0 comments on commit cffe317

Please sign in to comment.