diff options
author | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 16:31:08 +0530 |
---|---|---|
committer | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 16:31:08 +0530 |
commit | 57eb8f6712361a3bf75983ce153fac4846dc0273 (patch) | |
tree | 269a168d59c917c4e313c819e2b4c3ff8175f912 /cmd/web/controller/htmx/sign-in.go |
Initial commit
Diffstat (limited to 'cmd/web/controller/htmx/sign-in.go')
-rw-r--r-- | cmd/web/controller/htmx/sign-in.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/cmd/web/controller/htmx/sign-in.go b/cmd/web/controller/htmx/sign-in.go new file mode 100644 index 0000000..df60027 --- /dev/null +++ b/cmd/web/controller/htmx/sign-in.go @@ -0,0 +1,80 @@ +package HTMXController + +import ( + "errors" + "projecty/cmd/web/model" + "projecty/internal/authentication" + "projecty/internal/database" + "projecty/internal/helper" + + "github.com/gofiber/fiber/v2" + "gorm.io/gorm" +) + +func SignInPage(c *fiber.Ctx) error { + + return c.Render("sign-in/htmx-sign-in-page", fiber.Map{ + "PageTitle": "Sign In", + "NavBarActive": "sign-in", + "FiberCtx": c, + }, "layouts/app-htmx") + +} + +func SignInAction(c *fiber.Ctx) error { + + var user model.User + email := c.FormValue("email") + password := c.FormValue("password") + + if email == "" || password == "" { + + return c.Render("sign-in/partials/sign-in-form", fiber.Map{ + "Errors": []string{ + "Email or password cannot be null.", + }, + "IsOob": true, + }, "layouts/app-htmx") + } + + db := database.Get() + + db.Model(&user) + err := db.Where(&model.User{Email: email}). + First(&user).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return c.Render("sign-in/partials/sign-in-form", fiber.Map{ + "Errors": []string{ + "Email and password did not match.", + }, + }, "layouts/app-htmx") + } + } + + if !user.CheckPassword(password) { + + return c.Render("sign-in/partials/sign-in-form", fiber.Map{ + "Errors": []string{ + "Email and password did not match.", + }, + }, "layouts/app-htmx") + } + + authentication.AuthStore(c, user.ID) + + return helper.HTMXRedirectTo("/", "/htmx/home", c) +} + +func SignOut(c *fiber.Ctx) error { + + isAuthenticated, _ := authentication.AuthGet(c) + if !isAuthenticated { + return c.Redirect("/") + } + + authentication.AuthDestroy(c) + + return helper.HTMXRedirectTo("/", "/htmx/home", c) +} |