diff options
| author | Bobby <[email protected]> | 2026-03-07 14:25:54 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-07 14:25:54 +0530 |
| commit | 41926c10ea2e8496ce4b528262f5047ccbe6f155 (patch) | |
| tree | 67ff5a27bb50faec368cf7ef38a1b1f2866459dd /utils/auth | |
| parent | ed2a033d7c08e448f5c6fd035e2de8f51431b597 (diff) | |
| download | dove-41926c10ea2e8496ce4b528262f5047ccbe6f155.tar.xz dove-41926c10ea2e8496ce4b528262f5047ccbe6f155.zip | |
Implement authentication system with login/logout functionality and session management
Diffstat (limited to 'utils/auth')
| -rw-r--r-- | utils/auth/auth.go | 46 | ||||
| -rw-r--r-- | utils/auth/constants.go | 5 |
2 files changed, 51 insertions, 0 deletions
diff --git a/utils/auth/auth.go b/utils/auth/auth.go new file mode 100644 index 0000000..5abb496 --- /dev/null +++ b/utils/auth/auth.go @@ -0,0 +1,46 @@ +package auth + +import ( + "dove/session" + + "github.com/gofiber/fiber/v2" +) + +func IsAuthenticated(context *fiber.Ctx) bool { + activeSession, sessionError := session.Store.Get(context) + if sessionError != nil { + return false + } + + return activeSession.Get(SESSION_AUTHENTICATED_KEY) != nil +} + +func RequireAuthentication(handler fiber.Handler) fiber.Handler { + return func(context *fiber.Ctx) error { + switch IsAuthenticated(context) { + case true: + return handler(context) + default: + return fiber.ErrUnauthorized + } + } +} + +func Authenticate(context *fiber.Ctx) error { + activeSession, sessionError := session.Store.Get(context) + if sessionError != nil { + return sessionError + } + + activeSession.Set(SESSION_AUTHENTICATED_KEY, true) + return activeSession.Save() +} + +func Deauthenticate(context *fiber.Ctx) error { + activeSession, sessionError := session.Store.Get(context) + if sessionError != nil { + return sessionError + } + + return activeSession.Destroy() +} diff --git a/utils/auth/constants.go b/utils/auth/constants.go new file mode 100644 index 0000000..99f5a85 --- /dev/null +++ b/utils/auth/constants.go @@ -0,0 +1,5 @@ +package auth + +const ( + SESSION_AUTHENTICATED_KEY = "authenticated" +) |
