package main import ( "bytes" "encoding/json" "errors" "fmt" "net/http" ) func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) { var requestPayload struct { Email string `json:"email"` Password string `json:"password"` } err := app.readJSON(w, r, &requestPayload) if err != nil { app.errorJSON(w, err, http.StatusBadRequest) return } user, err := app.Models.User.GetByEmail(requestPayload.Email) if err != nil { // user not found app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest) return } valid, err := user.PasswordMatches(requestPayload.Password) if err != nil || !valid { app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest) return } // log authentication err = app.logRequest("authentication", fmt.Sprintf("%s is logged in", user.Email)) if err != nil { app.errorJSON(w, err) return } payload := jsonResponse{ Error: false, Message: fmt.Sprintf("%s %s is authorized to log in.", user.FirstName, user.LastName), Data: user, } app.writeJSON(w, http.StatusAccepted, payload) } func (app *Config) logRequest(name, data string) error { var entry struct { Name string `json:"name"` Data string `json:"data"` } entry.Name = name entry.Data = data jsonData, _ := json.MarshalIndent(entry, "", "\t") logServiceURL := "http://logger-service/log" request, err := http.NewRequest("POST", logServiceURL, bytes.NewBuffer(jsonData)) if err != nil { return err } request.Header.Add("Content-Type", "application/json") client := &http.Client{} _, err = client.Do(request) if err != nil { return err } return nil }