brunel/handlers/auth/isloggedin.go
2024-07-30 20:51:17 +01:00

30 lines
650 B
Go

package handlers_auth
import (
"brunel/auth"
"strings"
"github.com/gofiber/fiber/v2"
)
func IsLoggedIn(c *fiber.Ctx) error {
tokenPlusUsername := c.Cookies("pt")
if tokenPlusUsername == "" {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
split := strings.Split(tokenPlusUsername, ":")
token := split[0]
username := split[1]
ok, suser := auth.CheckSessionToken(token)
if !ok {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
if suser != username {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
return c.Status(fiber.StatusOK).SendString("Logged in")
}