package handler

import (
	"kra/internal/service"

	"github.com/gin-gonic/gin"
)

type Session struct{ tokens *service.TokenService }

func NewSession(tokens *service.TokenService) *Session { return &Session{tokens: tokens} }
func (h *Session) Logout(c *gin.Context) {
	token := c.GetHeader("x-token")
	if token == "" {
		token, _ = c.Cookie("x-token")
	}
	if err := h.tokens.BlacklistToken(c.Request.Context(), token); err != nil {
		Fail(c, "jwt作废失败")
		return
	}
	SetTokenCookie(c, "", -1)
	Write(c, CodeSuccess, gin.H{}, "jwt作废成功")
}
