summaryrefslogtreecommitdiff
path: root/nexus/utils/auth
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-29 22:52:46 +0530
committerBobby <[email protected]>2026-03-29 22:52:46 +0530
commit9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch)
treeda520b923b5e6758d5457b6233dd6671fc640914 /nexus/utils/auth
parent65a143a0871c35989b7c7ea6723d39a0585c089e (diff)
downloadechoes-of-vaelun-main.tar.xz
echoes-of-vaelun-main.zip
feat: nexus account manager scaffold with auth, characters, realmsHEADmain
Diffstat (limited to 'nexus/utils/auth')
-rw-r--r--nexus/utils/auth/auth.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/nexus/utils/auth/auth.go b/nexus/utils/auth/auth.go
new file mode 100644
index 0000000..4e44d5e
--- /dev/null
+++ b/nexus/utils/auth/auth.go
@@ -0,0 +1,54 @@
+package auth
+
+import (
+ "nexus/repositories/account"
+ "nexus/repositories/session"
+ "nexus/utils/meta"
+ "strings"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func APIAuth(handler fiber.Handler) fiber.Handler {
+ return func(context *fiber.Ctx) error {
+ authHeader := context.Get("Authorization")
+ if authHeader == "" {
+ return fiber.ErrUnauthorized
+ }
+
+ parts := strings.SplitN(authHeader, " ", 2)
+ if len(parts) != 2 || parts[0] != "Bearer" {
+ return fiber.ErrUnauthorized
+ }
+
+ s, err := session.FindByAuthToken(parts[1])
+ if err != nil || s == nil {
+ return fiber.ErrUnauthorized
+ }
+
+ if s.IsAuthExpired() {
+ return fiber.ErrUnauthorized
+ }
+
+ a, err := account.FindByID(s.AccountID)
+ if err != nil || a == nil {
+ return fiber.ErrUnauthorized
+ }
+
+ if !a.IsActive {
+ return fiber.ErrUnauthorized
+ }
+
+ context.Locals(meta.AccountKey, a)
+ return handler(context)
+ }
+}
+
+func WebAuth(handler fiber.Handler) fiber.Handler {
+ return func(context *fiber.Ctx) error {
+ if meta.Account(context) == nil {
+ return context.Redirect("/login")
+ }
+ return handler(context)
+ }
+}