2024-07-30 21:36:04 +02:00
|
|
|
package handlers_auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"brunel/auth"
|
2024-07-30 21:51:17 +02:00
|
|
|
"strings"
|
2024-07-30 21:36:04 +02:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func IsLoggedIn(c *fiber.Ctx) error {
|
2024-07-30 21:51:17 +02:00
|
|
|
|
|
|
|
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)
|
2024-07-30 21:36:04 +02:00
|
|
|
if !ok {
|
|
|
|
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
|
|
}
|
2024-07-30 21:51:17 +02:00
|
|
|
if suser != username {
|
|
|
|
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
|
|
}
|
|
|
|
|
2024-07-30 21:36:04 +02:00
|
|
|
return c.Status(fiber.StatusOK).SendString("Logged in")
|
|
|
|
}
|