summaryrefslogtreecommitdiff
path: root/utils/auth/auth.go
blob: 9f6e79d3f247e5c653624f65a3010a03bbd4118f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.login")
		}
		return handler(context)
	}
}