Fix some errors

This commit is contained in:
vinchent 2024-08-13 14:12:40 +02:00
parent bd5c6b2abc
commit d82d4c754e
3 changed files with 6 additions and 1 deletions

View File

@ -239,6 +239,7 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
err := app.readJSON(w, r, &userInput)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
@ -246,6 +247,7 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
// get the user from the db by email, send error if invalid email
user, err := app.DB.GetUserByEmail(userInput.Email)
if err != nil {
app.errorLog.Println(err)
app.invalidCredentials(w)
return
}
@ -253,6 +255,7 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
// validate the password, send error if invalid password
validPassword, err := app.passwordMatches(user.Password, userInput.Password)
if err != nil {
app.errorLog.Println(err)
app.invalidCredentials(w)
return
}

View File

@ -88,6 +88,7 @@ func (app *application) invalidCredentials(w http.ResponseWriter) error {
}
func (app *application) passwordMatches(hash, password string) (bool, error) {
app.errorLog.Println(hash, password)
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
switch {

View File

@ -244,11 +244,12 @@ func (m *DBModel) GetUserByEmail(email string) (User, error) {
&u.FirstName,
&u.LastName,
&u.Email,
&u.Password,
&u.CreatedAt,
&u.UpdatedAt,
)
if err != nil {
return User{}, err
return u, err
}
return u, nil