diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go index 39fbb4c..2813b6f 100644 --- a/cmd/api/handlers-api.go +++ b/cmd/api/handlers-api.go @@ -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) +} diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go new file mode 100644 index 0000000..4c29e4f --- /dev/null +++ b/cmd/api/helpers.go @@ -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 +} diff --git a/cmd/api/routes-api.go b/cmd/api/routes-api.go index 49bb804..ff14740 100644 --- a/cmd/api/routes-api.go +++ b/cmd/api/routes-api.go @@ -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 } diff --git a/static/js/login.js b/static/js/login.js index ea8ed9b..a3a2b2f 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -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(); diff --git a/tmp/air.log b/tmp/air.log new file mode 100644 index 0000000..b14dc6f --- /dev/null +++ b/tmp/air.log @@ -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 \ No newline at end of file