Socialify

Folder ..

Viewing auth.go
24 lines (20 loc) • 501.0 B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package auth

import "github.com/gofiber/fiber/v2"

func RequireAuth(c *fiber.Ctx) error {
	if !IsAuthenticated(c) {
		if c.Get("HX-Request") == "true" {
			c.Set("HX-Redirect", "/login")
			return c.SendStatus(fiber.StatusUnauthorized)
		}
		return c.Redirect("/login")
	}
	return c.Next()
}

func IsAuthenticated(c *fiber.Ctx) bool {
	auth, ok := c.Locals("authenticated").(bool)
	return ok && auth
}

func GetUserEmail(c *fiber.Ctx) string {
	email, _ := c.Locals("email").(string)
	return email
}