| 1234567891011121314151617181920212223242526272829303132 |
- package utils
- import (
- "context"
- "encoding/json"
- "github.com/golang-jwt/jwt/v4"
- )
- const Identify = "user_auth_id"
- func GetJwtToken(secretKey string, iat, seconds int64, uid int64) (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) int64 {
- if id, ok := ctx.Value(Identify).(json.Number); ok {
- i, _ := id.Int64()
- return i
- }
- if id, ok := ctx.Value(Identify).(float64); ok {
- return int64(id)
- }
- return 0
- }
|