diff options
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/constants.go | 8 | ||||
| -rw-r--r-- | controllers/home.go | 4 | ||||
| -rw-r--r-- | controllers/login.go | 24 | ||||
| -rw-r--r-- | controllers/logout.go | 23 | ||||
| -rw-r--r-- | controllers/register.go | 75 |
5 files changed, 112 insertions, 22 deletions
diff --git a/controllers/constants.go b/controllers/constants.go index 95d75c1..021eeb9 100644 --- a/controllers/constants.go +++ b/controllers/constants.go @@ -32,6 +32,10 @@ const ( ERR_LOGIN_INVALID_CREDENTIALS = `The credentials you provided are incorrect. Did you <a href="` + URL_FORGOT_PASSWORD + `">forget your password</a>?` ERR_ACCOUNT_DISABLED = `Your account is disabled or banned. You can reach out to support for assistance.` ERR_ACCOUNT_UNABLE_TO_LOGIN = `You cannot log in at this time. Verify your email or contact support. If you misplaced your verification email, you can <a href="` + URL_RESEND_VERIFICATION + `">request a new one</a>.` - ERR_SESSION_FAILED_TO_CREATE = "Failed to create session. Please try again later." - ERR_SESSION_FAILED_TO_SAVE = "Failed to save session. Please try again later." + ERR_PASSWORD_MISMATCH = "Entered passwords do not match. Ensure both fields are identical." + ERR_SESSION_FAILED_TO_CREATE = "Server failed to create a session. If this issue persists, contact support." + ERR_SESSION_FAILED_TO_SAVE = "Server failed to save session data. If this issue persists, contact support." + + // Success messages + SUCCESS_USER_REGISTERED = "Your account has been created successfully. A verification email has been sent to your email address. You will only be able to log in after verifying your email. If you did not receive the email, you can <a href=\"" + URL_RESEND_VERIFICATION + "\">request a new one</a>." ) diff --git a/controllers/home.go b/controllers/home.go index 6758a4b..186ef24 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -7,6 +7,6 @@ import ( ) func HomePageController(ctx *fiber.Ctx) error { - ctx.Locals("Title", "Home Page") - return shortcuts.Render(ctx, "home", nil) + ctx.Locals("Title", PT_HOME) + return shortcuts.Render(ctx, TEMPLATE_HOME, nil) } diff --git a/controllers/login.go b/controllers/login.go index 12262e3..6eb4996 100644 --- a/controllers/login.go +++ b/controllers/login.go @@ -3,17 +3,15 @@ package controllers import ( "imageboard/database" "imageboard/session" + "imageboard/utils/auth" "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 +type LoginForm struct { + Username string `json:"username" form:"username"` + Password string `json:"password" form:"password"` } func renderLoginError(ctx *fiber.Ctx, errorMsg string, statusCode int) error { @@ -25,11 +23,9 @@ func renderLoginError(ctx *fiber.Ctx, errorMsg string, statusCode int) error { func LoginPageController(ctx *fiber.Ctx) error { 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) - } + + if auth.IsAuthenticated(ctx) { + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) } return shortcuts.Render(ctx, TEMPLATE_LOGIN, nil) @@ -37,10 +33,6 @@ func LoginPageController(ctx *fiber.Ctx) error { 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 @@ -78,5 +70,5 @@ func LoginPostController(ctx *fiber.Ctx) error { user.UpdateLastUserLogin(database.DB) user.UpdateLastUserActivity(database.DB) - return ctx.Redirect(getRedirectURL(ctx), fiber.StatusSeeOther) + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) } diff --git a/controllers/logout.go b/controllers/logout.go new file mode 100644 index 0000000..20c280d --- /dev/null +++ b/controllers/logout.go @@ -0,0 +1,23 @@ +package controllers + +import ( + "imageboard/session" + "imageboard/utils/auth" + + "github.com/gofiber/fiber/v2" +) + +func LogoutController(ctx *fiber.Ctx) error { + sess, err := session.Store.Get(ctx) + if err != nil { + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) + } + + if err := sess.Destroy(); err != nil { + sess.Delete("user_id") + sess.Delete("username") + sess.Save() + } + + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) +} diff --git a/controllers/register.go b/controllers/register.go index d16db9d..acadbc5 100644 --- a/controllers/register.go +++ b/controllers/register.go @@ -1,12 +1,83 @@ package controllers import ( + "imageboard/database" + "imageboard/models" + "imageboard/utils/auth" + "imageboard/utils/email" "imageboard/utils/shortcuts" + "log" + "strings" "github.com/gofiber/fiber/v2" ) +type RegisterForm struct { + Username string `json:"username" form:"username"` + Email string `json:"email" form:"email"` + Password string `json:"password" form:"password"` + ConfirmPassword string `json:"confirm_password" form:"confirm_password"` +} + +func renderRegisterError(ctx *fiber.Ctx, errorMsg string, statusCode int) error { + return shortcuts.RenderWithStatus(ctx, TEMPLATE_REGISTER, fiber.Map{ + "Error": errorMsg, + "Username": ctx.FormValue("username"), + "Email": ctx.FormValue("email"), + }, statusCode) +} + func RegisterPageController(ctx *fiber.Ctx) error { - ctx.Locals("Title", "Register") - return shortcuts.Render(ctx, "register", nil) + ctx.Locals("Title", PT_REGISTER) + + if auth.IsAuthenticated(ctx) { + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) + } + + return shortcuts.Render(ctx, TEMPLATE_REGISTER, nil) +} + +func RegisterPostController(ctx *fiber.Ctx) error { + ctx.Locals("Title", PT_REGISTER) + + if auth.IsAuthenticated(ctx) { + return ctx.Redirect(auth.GetRedirectURL(ctx), fiber.StatusSeeOther) + } + + var form RegisterForm + if err := ctx.BodyParser(&form); err != nil { + return renderRegisterError(ctx, ERR_INVALID_FORM_DATA, fiber.StatusBadRequest) + } + + if form.Password != form.ConfirmPassword { + return renderRegisterError(ctx, ERR_PASSWORD_MISMATCH, fiber.StatusBadRequest) + } + + user := &models.User{ + Username: form.Username, + Email: form.Email, + Password: form.Password, + } + + if err := database.CreateUser(user); err != nil { + var statusCode int + if strings.Contains(err.Error(), "username") { + statusCode = fiber.StatusConflict + } else if strings.Contains(err.Error(), "email") { + statusCode = fiber.StatusBadRequest + } else { + statusCode = fiber.StatusInternalServerError + } + + return renderRegisterError(ctx, "Failed to create user: "+err.Error(), statusCode) + } + + if err := email.SendVerificationEmail(user); err != nil { + log.Printf("Failed to send verification email: %v", err) + return renderRegisterError(ctx, "User created but failed to send verification email", fiber.StatusInternalServerError) + } + + return shortcuts.Render(ctx, TEMPLATE_REGISTER, fiber.Map{ + "Success": SUCCESS_USER_REGISTERED, + }) } |
