This commit is contained in:
2024-08-13 13:47:56 +02:00
parent 011b3a8c0b
commit 4ec1d8c5a2
4 changed files with 59 additions and 0 deletions

View File

@ -243,6 +243,19 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
return
}
// get the user from the db by email, send error if invalid email
user, err := app.DB.GetUserByEmail(userInput.Email)
if err != nil {
app.invalidCredentials(w)
return
}
// validate the password, send error if invalid password
// generate the token
// send response
var payload struct {
Error bool `json:"error"`
Message string `json:"message"`

View File

@ -68,3 +68,18 @@ func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err e
w.Write(out)
return nil
}
func (app *application) invalidCredentials(w http.ResponseWriter) error {
var payload struct {
Error bool `json:"error"`
Message string `json:"message"`
}
payload.Error = true
payload.Message = "invalid authentication credentials"
err := app.writeJSON(w, http.StatusUnauthorized, payload)
if err != nil {
return err
}
return nil
}