JWT helpers and middleware(go fiber) for godor (got the door 😉)
go get -u github.com/arcvats/godor-jwt
godor-jwt provides helpers and middleware(fiber) for Encoding and Decoding a JWT token with HMAC methods
type Config struct {
Algorithm string // default is HS256
Secret string // required
Expiry int64 // default is 60 minutes
}
package main
import (
godorjwt "github.com/arcvats/godor-jwt"
)
func main() {
config := godorjwt.Config{
Algorithm: "HS256",
Secret: "secret",
Expiry: 60,
}
payload := map[string]any{
"sub": "1234567890",
"name": "John Doe",
"admin": true,
}
token, jti, expiry, error := godorjwt.Encode(payload, config)
}
package main
import (
godorjwt "github.com/arcvats/godor-jwt"
)
func main() {
config := godorjwt.Config{
Secret: "secret",
}
tokenString := "some.jwt.token"
decodedTokenClaims, error := godorjwt.Decode(tokenString, config)
}
package main
import (
godorjwt "github.com/arcvats/godor-jwt"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
config := godorjwt.New("secret")
// or
//config := godorjwt.Config{
// Secret: "secret",
//}
app.Use(config.Decoder())
/*
Token is acquired from the Authorization header or jwt Cookie
Now you can access the decoded token claims in the context
claims := c.Locals("decodedToken").(map[string]any)
*/
}
MIT