From a10358ff911c5f81dfdaedb1ba8227da424fa8f8 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 12 May 2024 09:35:13 -0700 Subject: [PATCH] users can now register with the service --- apiclient/users.go | 18 ++++++++++++++++++ internal/handlers/handler.go | 2 ++ internal/handlers/users.go | 20 ++++++++++++++++++++ internal/views/layout/withTemplate.templ | 2 +- internal/views/users/afterSignUp.templ | 15 +++++++++++++++ internal/views/users/signup.templ | 23 +++++++++++++++++++++++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 internal/views/users/afterSignUp.templ create mode 100644 internal/views/users/signup.templ diff --git a/apiclient/users.go b/apiclient/users.go index 552846b..bd4dfc3 100644 --- a/apiclient/users.go +++ b/apiclient/users.go @@ -15,6 +15,7 @@ const ( type Users interface { Login(username, password string) (domain.LoginResponse, error) + SignUp(username, password string) (domain.BaseResponse, error) } type userClient struct { @@ -45,3 +46,20 @@ func (a userClient) Login(username, password string) (domain.LoginResponse, erro return bind, nil } + +func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) { + endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserRegisterRoute) + + param := url.Values{} + param.Set("username", username) + param.Set("password", password) + + // Create the struct we are expecting back from the API so we can have it populated + var bind = domain.BaseResponse{} + err := PostUrlForm(a.client, endpoint, param, &bind) + if err != nil { + return domain.BaseResponse{}, err + } + + return bind, nil +} diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index 1e2c699..9f87d41 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -63,6 +63,8 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient. users := router.Group("/users") users.GET("/login", s.UserLogin) users.POST("/login", s.UserAfterLogin) + users.GET("/signup", s.UserSignUp) + users.POST("/signup", s.UserAfterSignUp) s.Router = router return s diff --git a/internal/handlers/users.go b/internal/handlers/users.go index 814fec9..65e2ead 100644 --- a/internal/handlers/users.go +++ b/internal/handlers/users.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "net/http" "git.jamestombleson.com/jtom38/newsbot-portal/internal/domain" @@ -38,3 +39,22 @@ func (h *Handler) UserAfterLogin(c echo.Context) error { return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true)) } + +func (h *Handler) UserSignUp(c echo.Context) error { + return Render(c, http.StatusOK, users.SignUp()) +} + +func (h *Handler) UserAfterSignUp(c echo.Context) error { + user := c.FormValue("username") + password := c.FormValue("password") + + resp, err := h.api.Users.SignUp(user, password) + if err != nil { + return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false)) + } + if resp.Message != "OK" { + msg := fmt.Sprintf("Failed to create account. Message: %s", resp.Message) + return Render(c, http.StatusBadRequest, users.AfterLogin(msg, false)) + } + return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true)) +} diff --git a/internal/views/layout/withTemplate.templ b/internal/views/layout/withTemplate.templ index 3bcfc45..23abbc4 100644 --- a/internal/views/layout/withTemplate.templ +++ b/internal/views/layout/withTemplate.templ @@ -61,7 +61,7 @@ templ WithTemplate() {