package token

import (
	"context"
	"github.com/golang-jwt/jwt"
)

const Identify = "imooc.com"

func GetJwtToken(secretKey string, iat, seconds int64, uid string) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = iat + seconds
	claims["iat"] = iat
	claims[Identify] = uid
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}

func GetUId(ctx context.Context) string {
	var uid string
	if jsonUid, ok := ctx.Value(Identify).(string); ok {
		uid = jsonUid
	}
	return uid
}
