aboutsummaryrefslogtreecommitdiff
path: root/controllers/login.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-07-13 11:57:45 +0530
committerBobby <[email protected]>2025-07-13 11:57:45 +0530
commitbf112649d039f8f02e2135a74d8b506f7c31c784 (patch)
tree0598be94ed9a718ac41bed1b8950c4887f381fef /controllers/login.go
parenta698f5fde54c96f017a5af600c1e54a20cf051e6 (diff)
downloadimageboard-bf112649d039f8f02e2135a74d8b506f7c31c784.tar.xz
imageboard-bf112649d039f8f02e2135a74d8b506f7c31c784.zip
Login post controller
Diffstat (limited to 'controllers/login.go')
-rw-r--r--controllers/login.go74
1 files changed, 72 insertions, 2 deletions
diff --git a/controllers/login.go b/controllers/login.go
index 1ea9caf..12262e3 100644
--- a/controllers/login.go
+++ b/controllers/login.go
@@ -1,12 +1,82 @@
package controllers
import (
+ "imageboard/database"
+ "imageboard/session"
"imageboard/utils/shortcuts"
"github.com/gofiber/fiber/v2"
)
+func getRedirectURL(ctx *fiber.Ctx) string {
+ referer := ctx.Get("Referer")
+ if referer != "" && referer != ctx.BaseURL()+URL_LOGIN && referer != ctx.BaseURL()+URL_REGISTER {
+ return referer
+ }
+ return URL_HOME
+}
+
+func renderLoginError(ctx *fiber.Ctx, errorMsg string, statusCode int) error {
+ return shortcuts.RenderWithStatus(ctx, TEMPLATE_LOGIN, fiber.Map{
+ "Error": errorMsg,
+ "Username": ctx.FormValue("username"), // Preserve username in form
+ }, statusCode)
+}
+
func LoginPageController(ctx *fiber.Ctx) error {
- ctx.Locals("Title", "Login")
- return shortcuts.Render(ctx, "login", nil)
+ ctx.Locals("Title", PT_LOGIN)
+ sess, err := session.Store.Get(ctx)
+ if err == nil {
+ if userID, ok := sess.Get("user_id").(int); ok && userID != 0 {
+ return ctx.Redirect(getRedirectURL(ctx), fiber.StatusSeeOther)
+ }
+ }
+
+ return shortcuts.Render(ctx, TEMPLATE_LOGIN, nil)
+}
+
+func LoginPostController(ctx *fiber.Ctx) error {
+ ctx.Locals("Title", PT_LOGIN)
+ type LoginForm struct {
+ Username string `json:"username" form:"username"`
+ Password string `json:"password" form:"password"`
+ }
+
+ var form LoginForm
+ var err error
+ if err = ctx.BodyParser(&form); err != nil {
+ return renderLoginError(ctx, ERR_INVALID_FORM_DATA, fiber.StatusBadRequest)
+ }
+
+ user, err := database.GetUserByUsername(form.Username)
+ if err != nil {
+ return renderLoginError(ctx, ERR_USER_NOT_FOUND, fiber.StatusUnauthorized)
+ }
+
+ if !user.CheckPassword(form.Password) {
+ return renderLoginError(ctx, ERR_LOGIN_INVALID_CREDENTIALS, fiber.StatusUnauthorized)
+ }
+
+ if !user.IsActive() {
+ return renderLoginError(ctx, ERR_ACCOUNT_DISABLED, fiber.StatusForbidden)
+ }
+
+ if !user.CanLogin() {
+ return renderLoginError(ctx, ERR_ACCOUNT_UNABLE_TO_LOGIN, fiber.StatusForbidden)
+ }
+
+ sess, err := session.Store.Get(ctx)
+ if err != nil {
+ return renderLoginError(ctx, ERR_SESSION_FAILED_TO_CREATE, fiber.StatusInternalServerError)
+ }
+ sess.Set("user_id", user.ID)
+ sess.Set("username", user.Username)
+ if err := sess.Save(); err != nil {
+ return renderLoginError(ctx, ERR_SESSION_FAILED_TO_SAVE, fiber.StatusInternalServerError)
+ }
+
+ user.UpdateLastUserLogin(database.DB)
+ user.UpdateLastUserActivity(database.DB)
+
+ return ctx.Redirect(getRedirectURL(ctx), fiber.StatusSeeOther)
}