diff options
| author | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
| commit | 9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch) | |
| tree | da520b923b5e6758d5457b6233dd6671fc640914 /nexus/controllers/auth | |
| parent | 65a143a0871c35989b7c7ea6723d39a0585c089e (diff) | |
| download | echoes-of-vaelun-main.tar.xz echoes-of-vaelun-main.zip | |
Diffstat (limited to 'nexus/controllers/auth')
| -rw-r--r-- | nexus/controllers/auth/defaults.go | 3 | ||||
| -rw-r--r-- | nexus/controllers/auth/login.go | 39 | ||||
| -rw-r--r-- | nexus/controllers/auth/logout.go | 20 | ||||
| -rw-r--r-- | nexus/controllers/auth/messages.go | 6 | ||||
| -rw-r--r-- | nexus/controllers/auth/register.go | 48 |
5 files changed, 116 insertions, 0 deletions
diff --git a/nexus/controllers/auth/defaults.go b/nexus/controllers/auth/defaults.go new file mode 100644 index 0000000..8998b77 --- /dev/null +++ b/nexus/controllers/auth/defaults.go @@ -0,0 +1,3 @@ +package auth
+
+const LogPrefix = "AuthController"
diff --git a/nexus/controllers/auth/login.go b/nexus/controllers/auth/login.go new file mode 100644 index 0000000..4fd7ffd --- /dev/null +++ b/nexus/controllers/auth/login.go @@ -0,0 +1,39 @@ +package auth
+
+import (
+ "nexus/services/auth"
+ "nexus/sessions"
+ authTypes "nexus/types/auth"
+ "nexus/utils/collections"
+ "nexus/utils/meta"
+ "nexus/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Login(context *fiber.Ctx) error {
+ body, err := meta.Body[authTypes.LoginRequest](context)
+ if err != nil {
+ return shortcuts.RedirectWithFlash(context, "login", collections.Record[string, any]{
+ "Error": err.Error(),
+ })
+ }
+
+ req := meta.Request(context)
+
+ session, serviceErr := auth.Login(body, req.IP, req.Header("User-Agent"))
+ if serviceErr != nil {
+ return shortcuts.RedirectWithFlash(context, "login", collections.Record[string, any]{
+ "Error": serviceErr.Message,
+ })
+ }
+
+ sess := meta.Session(context)
+ if err := sessions.CreateSession(sess, session.AccountID); err != nil {
+ return shortcuts.RedirectWithFlash(context, "login", collections.Record[string, any]{
+ "Error": ErrSessionCreate,
+ })
+ }
+
+ return shortcuts.Redirect(context, "account")
+}
diff --git a/nexus/controllers/auth/logout.go b/nexus/controllers/auth/logout.go new file mode 100644 index 0000000..f4c7014 --- /dev/null +++ b/nexus/controllers/auth/logout.go @@ -0,0 +1,20 @@ +package auth
+
+import (
+ "nexus/sessions"
+ "nexus/utils/collections"
+ "nexus/utils/meta"
+ "nexus/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Logout(context *fiber.Ctx) error {
+ sess := meta.Session(context)
+ if err := sessions.DestroySession(sess); err != nil {
+ return shortcuts.RedirectWithFlash(context, "account", collections.Record[string, any]{
+ "Error": ErrSessionDestroy,
+ })
+ }
+ return shortcuts.Redirect(context, "login")
+}
diff --git a/nexus/controllers/auth/messages.go b/nexus/controllers/auth/messages.go new file mode 100644 index 0000000..de8f0bd --- /dev/null +++ b/nexus/controllers/auth/messages.go @@ -0,0 +1,6 @@ +package auth
+
+const (
+ ErrSessionCreate = "Failed to create session. Please try again."
+ ErrSessionDestroy = "Failed to logout. Please try again."
+)
diff --git a/nexus/controllers/auth/register.go b/nexus/controllers/auth/register.go new file mode 100644 index 0000000..6436073 --- /dev/null +++ b/nexus/controllers/auth/register.go @@ -0,0 +1,48 @@ +package auth
+
+import (
+ "nexus/services/auth"
+ "nexus/sessions"
+ authTypes "nexus/types/auth"
+ "nexus/utils/collections"
+ "nexus/utils/meta"
+ "nexus/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Register(context *fiber.Ctx) error {
+ body, err := meta.Body[authTypes.RegisterRequest](context)
+ if err != nil {
+ return shortcuts.RedirectWithFlash(context, "register", collections.Record[string, any]{
+ "Error": err.Error(),
+ })
+ }
+
+ req := meta.Request(context)
+
+ account, serviceErr := auth.Register(body)
+ if serviceErr != nil {
+ return shortcuts.RedirectWithFlash(context, "register", collections.Record[string, any]{
+ "Error": serviceErr.Message,
+ })
+ }
+
+ session, serviceErr := auth.Login(authTypes.LoginRequest{
+ Email: body.Email,
+ Password: body.Password,
+ }, req.IP, req.Header("User-Agent"))
+ if serviceErr != nil {
+ return shortcuts.Redirect(context, "login")
+ }
+
+ sess := meta.Session(context)
+ if err := sessions.CreateSession(sess, account.ID); err != nil {
+ return shortcuts.RedirectWithFlash(context, "login", collections.Record[string, any]{
+ "Error": ErrSessionCreate,
+ })
+ }
+
+ _ = session
+ return shortcuts.Redirect(context, "account")
+}
|
