Create route and handler for authentication
This commit is contained in:
parent
aab0ef380a
commit
b37dcf89fd
@ -230,3 +230,28 @@ func (app *application) SaveOrder(order models.Order) (int, error) {
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request) {
|
||||
var userInput struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &userInput)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
payload.Error = false
|
||||
payload.Message = "Success!"
|
||||
|
||||
out, _ := json.MarshalIndent(payload, "", "\t")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
47
cmd/api/helpers.go
Normal file
47
cmd/api/helpers.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) readJSON(w http.ResponseWriter, r *http.Request, data interface{}) error {
|
||||
maxBytes := 1048576
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes))
|
||||
|
||||
dec := json.NewDecoder(r.Body)
|
||||
err := dec.Decode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make sure there is only one entry.
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
return errors.New("body must only have a single JSON value")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error {
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
payload.Error = true
|
||||
payload.Message = err.Error()
|
||||
|
||||
out, err := json.MarshalIndent(payload, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
return nil
|
||||
}
|
@ -21,5 +21,8 @@ func (app *application) routes() http.Handler {
|
||||
mux.Post("/api/payment-intent", app.GetPaymentIntent)
|
||||
mux.Get("/api/widget/{id}", app.GetWidgetByID)
|
||||
mux.Post("/api/create-customer-and-subscribe-to-plan", app.CreateCustomerAndSubscribeToPlan)
|
||||
|
||||
mux.Post("/api/authenticate", app.CreateAuthToken)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
export function val(api) {
|
||||
let form = document.getElementById("login_form");
|
||||
let form = document.getElementById("login-form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
this.event.preventDefault();
|
||||
|
1
tmp/air.log
Normal file
1
tmp/air.log
Normal file
@ -0,0 +1 @@
|
||||
exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2
|
Loading…
Reference in New Issue
Block a user