Socialify

Folder ..

Viewing auth.go
37 lines (27 loc) • 660.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
25
26
27
28
29
30
31
32
33
34
35
36
37
package middleware

import (
	"pagoda/auth"

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

type Session struct {
	SessionId string `cookie:"session"`
}

func Authenticated(c *fiber.Ctx) error {
	authHeader := c.Get("Authorization")

	// check if the header is empty
	if authHeader == "" {
		return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
			"error": "Unauthorized",
		})
	}

	user, err := auth.GetSession(authHeader)

	// user, err := auth.GetSession(session.SessionId)

	if err != nil {
		return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
			"error": "Unauthorized",
		})
	}

	// set the user in the context
	c.Locals("user", user)

	return c.Next()
}