package interceptor

import (
	"net/http"

	"bindbox-game/configs"
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/jwtoken"
	"bindbox-game/internal/proposal"
)

func (i *interceptor) AppTokenAuthVerify(ctx core.Context) (sessionUserInfo proposal.SessionUserInfo, err core.BusinessError) {
	headerAuthorizationString := ctx.GetHeader("Authorization")
	if headerAuthorizationString == "" {
		err = core.Error(
			http.StatusUnauthorized,
			code.JWTAuthVerifyError,
			"无法确认您的身份，请进行登录。",
		)
		return
	}

	jwtClaims, jwtErr := jwtoken.New(configs.Get().JWT.PatientSecret).Parse(headerAuthorizationString)
	if jwtErr != nil {
		err = core.Error(
			http.StatusUnauthorized,
			code.JWTAuthVerifyError,
			"您的账号登录过期，请重新登录。",
		)
		return
	}

	sessionUserInfo = jwtClaims.SessionUserInfo
	return
}
