summaryrefslogtreecommitdiff
path: root/utils/auth
diff options
context:
space:
mode:
Diffstat (limited to 'utils/auth')
-rw-r--r--utils/auth/auth.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/utils/auth/auth.go b/utils/auth/auth.go
new file mode 100644
index 0000000..3cee3a4
--- /dev/null
+++ b/utils/auth/auth.go
@@ -0,0 +1,27 @@
+package auth
+
+import (
+ "cafe/session"
+ "cafe/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func IsAuthenticated(context *fiber.Ctx) bool {
+ session, err := session.Store.Get(context)
+ if err != nil {
+ return false
+ }
+
+ username := session.Get("username")
+ return username != nil
+}
+
+func RequireAuthentication(handler fiber.Handler) fiber.Handler {
+ return func(context *fiber.Ctx) error {
+ if !IsAuthenticated(context) {
+ return shortcuts.Redirect(context, "auth.authenticate")
+ }
+ return handler(context)
+ }
+}