From 41926c10ea2e8496ce4b528262f5047ccbe6f155 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sat, 7 Mar 2026 14:25:54 +0530 Subject: Implement authentication system with login/logout functionality and session management --- utils/auth/auth.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ utils/auth/constants.go | 5 +++++ 2 files changed, 51 insertions(+) create mode 100644 utils/auth/auth.go create mode 100644 utils/auth/constants.go (limited to 'utils/auth') 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" +) -- cgit v1.2.3