From 668ed978ba65d03bc8b4d6195ddc29dba2110b34 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 12 May 2024 09:22:14 -0700 Subject: [PATCH] Was able to login and get a response from the api --- internal/handlers/handler.go | 22 +++++++++------ internal/handlers/users.go | 40 +++++++++++++++++++++++++++ internal/views/users/afterLogin.templ | 15 ++++++++++ internal/views/users/login.templ | 23 +++++++++++++++ 4 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 internal/handlers/users.go create mode 100644 internal/views/users/afterLogin.templ create mode 100644 internal/views/users/login.templ diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index 7aa62fe..1e2c699 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -7,16 +7,10 @@ import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - _ "git.jamestombleson.com/jtom38/newsbot-api/docs" + "git.jamestombleson.com/jtom38/newsbot-portal/apiclient" "git.jamestombleson.com/jtom38/newsbot-portal/internal/config" - //"git.jamestombleson.com/jtom38/newsbot-api/domain" ) -type Handler struct { - Router *echo.Echo - config config.Configs -} - const ( ErrParameterIdMissing = "The requested parameter ID was not found." ErrParameterMissing = "The requested parameter was not found found:" @@ -39,9 +33,16 @@ var ( ErrUnableToConvertToJson string = "Unable to convert to json" ) -func NewServer(ctx context.Context, configs config.Configs) *Handler { +type Handler struct { + Router *echo.Echo + config config.Configs + api apiclient.ApiClient +} + +func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.ApiClient) *Handler { s := &Handler{ config: configs, + api: apiClient, } //jwtConfig := echojwt.Config{ @@ -57,6 +58,11 @@ func NewServer(ctx context.Context, configs config.Configs) *Handler { router.Pre(middleware.Recover()) router.GET("/", s.HomeIndex) + router.GET("/about", s.HomeAbout) + + users := router.Group("/users") + users.GET("/login", s.UserLogin) + users.POST("/login", s.UserAfterLogin) s.Router = router return s diff --git a/internal/handlers/users.go b/internal/handlers/users.go new file mode 100644 index 0000000..814fec9 --- /dev/null +++ b/internal/handlers/users.go @@ -0,0 +1,40 @@ +package handlers + +import ( + "net/http" + + "git.jamestombleson.com/jtom38/newsbot-portal/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/users" + "github.com/labstack/echo/v4" +) + +func (h *Handler) UserLogin(c echo.Context) error { + return Render(c, http.StatusOK, users.Login()) +} + +func (h *Handler) UserAfterLogin(c echo.Context) error { + user := c.FormValue("username") + password := c.FormValue("password") + + resp, err := h.api.Users.Login(user, password) + if err != nil { + return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false)) + } + + cookie := new(http.Cookie) + cookie.Name = domain.CookieToken + cookie.Value = resp.Token + c.SetCookie(cookie) + + cookie = new(http.Cookie) + cookie.Name = domain.CookieRefreshToken + cookie.Value = resp.RefreshToken + c.SetCookie(cookie) + + cookie = new(http.Cookie) + cookie.Name = domain.CookieUser + cookie.Value = user + c.SetCookie(cookie) + + return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true)) +} diff --git a/internal/views/users/afterLogin.templ b/internal/views/users/afterLogin.templ new file mode 100644 index 0000000..8025e9a --- /dev/null +++ b/internal/views/users/afterLogin.templ @@ -0,0 +1,15 @@ +package users + +// This is returned after the user logs into the application. +// It just returns a partial view because it will overlap with the existing template. +templ AfterLogin(message string, success bool) { + if success { +
+ { message } +
+ } else { +
+ { message } +
+ } +} diff --git a/internal/views/users/login.templ b/internal/views/users/login.templ new file mode 100644 index 0000000..678750c --- /dev/null +++ b/internal/views/users/login.templ @@ -0,0 +1,23 @@ +package users + +import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout" + +templ Login() { + @layout.WithTemplate() { +
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+ } +}