Update JWT documentation for BeginTX pattern
Updated all code examples to reflect the new generator API that accepts a BeginTX function instead of managing database connections directly. Changes include: - Updated Basic Setup example with txGetter function - Updated Without Database example - Updated GORM integration example - Updated Bun integration example - Updated Complete Example 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
34
JWT.md
34
JWT.md
@@ -27,6 +27,7 @@ go get git.haelnorr.com/h/golib/jwt
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"git.haelnorr.com/h/golib/jwt"
|
||||
_ "github.com/lib/pq"
|
||||
@@ -40,6 +41,11 @@ func main() {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Create a transaction getter function
|
||||
txGetter := func(ctx context.Context) (jwt.DBTransaction, error) {
|
||||
return db.Begin()
|
||||
}
|
||||
|
||||
// Create token generator
|
||||
gen, err := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
AccessExpireAfter: 15, // Access tokens expire in 15 minutes
|
||||
@@ -53,7 +59,7 @@ func main() {
|
||||
Version: "15",
|
||||
},
|
||||
TableConfig: jwt.DefaultTableConfig(),
|
||||
})
|
||||
}, txGetter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -74,8 +80,8 @@ gen, err := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
FreshExpireAfter: 5,
|
||||
TrustedHost: "example.com",
|
||||
SecretKey: "your-secret-key-here",
|
||||
DBConn: nil, // No database = no revocation
|
||||
})
|
||||
DB: nil, // No database = no revocation
|
||||
}, nil) // nil transaction getter since no DB
|
||||
```
|
||||
|
||||
## Configuration
|
||||
@@ -389,6 +395,7 @@ GORM transactions can be used directly with the JWT package:
|
||||
|
||||
```go
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/driver/postgres"
|
||||
)
|
||||
@@ -397,6 +404,11 @@ import (
|
||||
gormDB, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
sqlDB, _ := gormDB.DB()
|
||||
|
||||
// Create a transaction getter function
|
||||
txGetter := func(ctx context.Context) (jwt.DBTransaction, error) {
|
||||
return sqlDB.Begin()
|
||||
}
|
||||
|
||||
// Create generator with sql.DB
|
||||
gen, _ := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
AccessExpireAfter: 15,
|
||||
@@ -410,7 +422,7 @@ gen, _ := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
Version: "15",
|
||||
},
|
||||
TableConfig: jwt.DefaultTableConfig(),
|
||||
})
|
||||
}, txGetter)
|
||||
|
||||
// Option 1: Use GORM transactions directly
|
||||
tx := gormDB.Begin()
|
||||
@@ -443,6 +455,11 @@ sqlDB := sql.Open(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
||||
// Pass sql.DB to both Bun and JWT generator
|
||||
bunDB := bun.NewDB(sqlDB, pgdialect.New())
|
||||
|
||||
// Create a transaction getter function
|
||||
txGetter := func(ctx context.Context) (jwt.DBTransaction, error) {
|
||||
return sqlDB.Begin()
|
||||
}
|
||||
|
||||
// Create generator with the same sql.DB
|
||||
gen, _ := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
AccessExpireAfter: 15,
|
||||
@@ -456,7 +473,7 @@ gen, _ := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
Version: "15",
|
||||
},
|
||||
TableConfig: jwt.DefaultTableConfig(),
|
||||
})
|
||||
}, txGetter)
|
||||
|
||||
// Option 1: Use Bun transactions directly (recommended)
|
||||
ctx := context.Background()
|
||||
@@ -494,6 +511,11 @@ func main() {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Create a transaction getter function
|
||||
txGetter := func(ctx context.Context) (jwt.DBTransaction, error) {
|
||||
return db.Begin()
|
||||
}
|
||||
|
||||
// Create token generator
|
||||
gen, err := jwt.CreateGenerator(jwt.GeneratorConfig{
|
||||
AccessExpireAfter: 15,
|
||||
@@ -507,7 +529,7 @@ func main() {
|
||||
Version: "15",
|
||||
},
|
||||
TableConfig: jwt.DefaultTableConfig(),
|
||||
})
|
||||
}, txGetter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user