Socialify

Folder ..

Viewing auth.go
24 lines (20 loc) • 471.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 middleware

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/session"
)

func AuthMiddleware(store *session.Store) fiber.Handler {
	return func(c *fiber.Ctx) error {
		session, err := store.Get(c)
		if err != nil {
			return c.Next()
		}

		if email, ok := session.Get("email").(string); ok {
			c.Locals("email", email)
			c.Locals("authenticated", true)
		} else {
			c.Locals("authenticated", false)
		}

		return c.Next()
	}
}