Auth
This commit is contained in:
@ -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"`
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user