aboutsummaryrefslogtreecommitdiff
path: root/utils/auth
diff options
context:
space:
mode:
Diffstat (limited to 'utils/auth')
-rw-r--r--utils/auth/auth.go46
-rw-r--r--utils/auth/constants.go5
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"
+)