brunel/handlers/auth/login.go

44 lines
932 B
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package handlers_auth
import (
"brunel/auth"
"brunel/config"
"time"
"github.com/gofiber/fiber/v2"
)
func Login(c *fiber.Ctx) error {
username := c.FormValue("username")
password := c.FormValue("password")
ok, err := auth.VerifyPassword(username, password)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
if !ok {
return c.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
}
token, err := auth.GenerateAndStoreSessionToken(username)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
tokenCookie := fiber.Cookie{
Name: "pt",
Value: token + ":" + username,
Domain: config.Configs.Hostname,
Expires: time.Now().Add(24 * time.Hour),
Secure: true,
HTTPOnly: true,
SameSite: "lax",
}
c.Cookie(&tokenCookie)
return c.Status(fiber.StatusOK).SendString("Login")
}