diff --git a/lib/service.go b/lib/service.go index 068a870..d56547c 100644 --- a/lib/service.go +++ b/lib/service.go @@ -1,16 +1,11 @@ package lib import ( - "github.com/getAlby/lndhub.go/db/models" - "github.com/labstack/echo/v4" "github.com/lightningnetwork/lnd/lnrpc" "github.com/uptrace/bun" ) type LndhubService struct { - echo.Context - DB *bun.DB - User *models.User LndClient *lnrpc.LightningClient } diff --git a/lib/tokens/jwt.go b/lib/tokens/jwt.go index 5b47500..61b4ba1 100644 --- a/lib/tokens/jwt.go +++ b/lib/tokens/jwt.go @@ -1,18 +1,13 @@ package tokens import ( - "context" - "database/sql" - "errors" "net/http" "time" "github.com/getAlby/lndhub.go/db/models" - "github.com/getAlby/lndhub.go/lib" "github.com/golang-jwt/jwt" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "github.com/uptrace/bun" ) type jwtCustomClaims struct { @@ -45,30 +40,6 @@ func Middleware(secret []byte) echo.MiddlewareFunc { return middleware.JWTWithConfig(config) } -func UserMiddleware(db *bun.DB) echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - ctx := c.(*lib.LndhubService) - userId := c.Get("UserID") - - var user models.User - - err := db.NewSelect().Model(&user).Where("id = ?", userId).Limit(1).Scan(context.TODO()) - switch { - case errors.Is(err, sql.ErrNoRows): - return echo.NewHTTPError(http.StatusNotFound, "user with given ID is not found") - case err != nil: - c.Logger().Errorf("database error: %v", err) - return echo.NewHTTPError(http.StatusInternalServerError) - } - - ctx.User = &user - - return next(ctx) - } - } -} - // GenerateAccessToken : Generate Access Token func GenerateAccessToken(secret []byte, expiryInSeconds int, u *models.User) (string, error) { claims := &jwtCustomClaims{ diff --git a/main.go b/main.go index c9cf4eb..75e8c5a 100644 --- a/main.go +++ b/main.go @@ -112,18 +112,10 @@ func main() { } logger.Infof("Connected to LND: %s - %s", getInfo.Alias, getInfo.IdentityPubkey) - // Initialize a custom context with - e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - cc := &lib.LndhubService{Context: c, DB: dbConn, LndClient: &lndClient} - return next(cc) - } - }) - e.POST("/auth", controllers.AuthController{JWTSecret: c.JWTSecret, JWTExpiry: c.JWTExpiry}.Auth) e.POST("/create", controllers.CreateUserController{}.CreateUser) - secured := e.Group("", tokens.Middleware(c.JWTSecret), tokens.UserMiddleware(dbConn)) + secured := e.Group("", tokens.Middleware(c.JWTSecret)) secured.POST("/addinvoice", controllers.AddInvoiceController{}.AddInvoice) secured.POST("/payinvoice", controllers.PayInvoiceController{}.PayInvoice) secured.GET("/gettxs", controllers.GetTXSController{}.GetTXS)