refactor to improve database operability
This commit is contained in:
@@ -28,6 +28,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"
|
||||
@@ -38,8 +39,10 @@ func main() {
|
||||
db, _ := sql.Open("postgres", "postgres://user:pass@localhost/db")
|
||||
defer db.Close()
|
||||
|
||||
// Wrap database connection
|
||||
dbConn := jwt.NewDBConnection(db)
|
||||
// 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{
|
||||
@@ -48,13 +51,13 @@ func main() {
|
||||
FreshExpireAfter: 5, // 5 minutes
|
||||
TrustedHost: "example.com",
|
||||
SecretKey: "your-secret-key",
|
||||
DBConn: dbConn,
|
||||
DB: db,
|
||||
DBType: jwt.DatabaseType{
|
||||
Type: jwt.DatabasePostgreSQL,
|
||||
Version: "15",
|
||||
},
|
||||
TableConfig: jwt.DefaultTableConfig(),
|
||||
})
|
||||
}, txGetter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -64,7 +67,7 @@ func main() {
|
||||
refreshToken, _, _ := gen.NewRefresh(42, false)
|
||||
|
||||
// Validate token
|
||||
tx, _ := dbConn.BeginTx(context.Background(), nil)
|
||||
tx, _ := db.Begin()
|
||||
token, _ := gen.ValidateAccess(tx, accessToken)
|
||||
|
||||
// Revoke token
|
||||
|
||||
Reference in New Issue
Block a user