phoenix/jwttoken/token.go
Ivan R. 6d25c4e8af
feat!: migrate to net/http
With the release of Go 1.22, the standard library now has
all the necessary functions that allow us to abandon Gin.

I hope this rewrite will lower the entry barrier for new developers.
As a nice bonus, the size of the program has decreased from 20 to 15.4 MB.

To solve issue #81, request logging has been improved.
Now all errors are displayed in the logs.
2024-03-25 15:52:18 +05:00

34 lines
731 B
Go

package jwttoken
import (
"net/http"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/ordinary-dev/phoenix/config"
)
const (
TOKEN_LIFETIME_IN_SECONDS = 60 * 60 * 24 * 30
TOKEN_COOKIE_NAME = "phoenix-token"
)
func GetJWTToken() (string, error) {
claims := jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Second * TOKEN_LIFETIME_IN_SECONDS)),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(config.Cfg.SecretKey))
}
func TokenToCookie(value string) *http.Cookie {
return &http.Cookie{
Name: TOKEN_COOKIE_NAME,
Value: value,
HttpOnly: true,
Secure: config.Cfg.SecureCookie,
MaxAge: TOKEN_LIFETIME_IN_SECONDS,
}
}