dgrijalva / jwt-go

ARCHIVE - Golang implementation of JSON Web Tokens (JWT). This project is now maintained at:
https://github.com/golang-jwt/jwt
MIT License
10.78k stars 994 forks source link

cannot use time.Now().Add(time.Hour * 24).Unix() (type int64) as type *jwt.Time in field value #487

Open encryptblockr opened 2 years ago

encryptblockr commented 2 years ago

Why am i getting this error?

cannot use time.Now().Add(time.Hour * 24).Unix() (type int64) as type *jwt.Time in field value

Here is what i have

import (
    "github.com/gofiber/fiber/v2"
    "golang.org/x/crypto/bcrypt"
    "github.com/dgrijalva/jwt-go/v4"
    "strconv"
    "time"
)

    payload := jwt.StandardClaims{
        Subject: strconv.Itoa(int(user.Id)),
        ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
    }

    token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, payload).SignedString([]byte("secret"))

What am i doing wrong here???

issue is on this particular line

        ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
encryptblockr commented 2 years ago

I have also tried the following

    payload := &jwt.StandardClaims{
        Subject: strconv.Itoa(int(user.Id)),
        ExpiresAt: time.Now().Add(time.Hour * 24),
    }

i get this error

cannot use time.Now().Add(time.Hour * 24) (type time.Time) as type *jwt.Time in field value

funny thing is i see this as part of the examples here https://github.com/dgrijalva/jwt-go/blob/master/example_test.go#L18 but yet does not work for me

    payload := &jwt.StandardClaims{
        Subject: strconv.Itoa(int(user.Id)),
        ExpiresAt: 3600,
    }

i get this error

cannot use 3600 (type int) as type *jwt.Time in field value
levniko commented 2 years ago

could you fixed that error. I got same

naruepanart commented 2 years ago

try change import from "github.com/dgrijalva/jwt-go/v4" to "github.com/dgrijalva/jwt-go"