package server import ( "encoding/json" "io" "net/http" ) type ( userLogin struct { Username string `json:"username"` Password string `json:"password"` } userRegistration struct { UserUsername string `json:"username"` UserPassword string `json:"password"` UserDisplayName string `json:"displayName"` } userPresenter struct { ID string `json:"id"` Username string `json:"username"` DisplayName string `json:"displayName"` } jwtPresenter struct { Token []byte `json:"token"` } ) func (ur userRegistration) Username() string { return ur.UserUsername } func (ur userRegistration) Password() string { return ur.UserPassword } func (ur userRegistration) DisplayName() string { return ur.UserDisplayName } func (s *HTTPServer) loginUserHandler(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { panic(err) } var ul userLogin err = json.Unmarshal(body, &ul) if err != nil { panic(err) } jwt, err := s.deps.Authenticator.Authenticate(ul.Username, ul.Password) if err != nil { unauthorized(w, r) return } payload := jwtPresenter{ Token: jwt, } ok(payload, w) } func (s *HTTPServer) registerUserHandler(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { panic(err) } var ur userRegistration err = json.Unmarshal(body, &ur) if err != nil { panic(err) } u, err := s.deps.UserRepository.CreateUser(ur) if err != nil { panic(err) } payload := userPresenter{ ID: u.ID().String(), Username: u.Username(), DisplayName: u.DisplayName(), } ok(payload, w) }