refactor auth

This commit is contained in:
kiwiidb
2022-01-19 14:12:53 +01:00
parent 860e2fa402
commit c0644b1da7

View File

@@ -15,11 +15,11 @@ import (
type AuthController struct { type AuthController struct {
JWTSecret []byte JWTSecret []byte
JWTExpiry int JWTExpiry int
svc *lib.LndhubService
} }
// Auth : Auth Controller // Auth : Auth Controller
func (ctrl AuthController) Auth(c echo.Context) error { func (controller *AuthController) Auth(c echo.Context) error {
ctx := c.(*lib.LndhubService)
type RequestBody struct { type RequestBody struct {
Login string `json:"login"` Login string `json:"login"`
Password string `json:"password"` Password string `json:"password"`
@@ -40,13 +40,12 @@ func (ctrl AuthController) Auth(c echo.Context) error {
}) })
} }
db := ctx.DB
var user models.User var user models.User
switch { switch {
case body.Login != "" || body.Password != "": case body.Login != "" || body.Password != "":
{ {
if err := db.NewSelect().Model(&user).Where("login = ?", body.Login).Scan(context.TODO()); err != nil { if err := controller.svc.DB.NewSelect().Model(&user).Where("login = ?", body.Login).Scan(context.TODO()); err != nil {
return c.JSON(http.StatusNotFound, echo.Map{ return c.JSON(http.StatusNotFound, echo.Map{
"error": true, "error": true,
"code": 1, "code": 1,
@@ -81,12 +80,12 @@ func (ctrl AuthController) Auth(c echo.Context) error {
}) })
} }
accessToken, err := tokens.GenerateAccessToken(ctrl.JWTSecret, ctrl.JWTExpiry, &user) accessToken, err := tokens.GenerateAccessToken(controller.JWTSecret, controller.JWTExpiry, &user)
if err != nil { if err != nil {
return err return err
} }
refreshToken, err := tokens.GenerateRefreshToken(ctrl.JWTSecret, ctrl.JWTExpiry, &user) refreshToken, err := tokens.GenerateRefreshToken(controller.JWTSecret, controller.JWTExpiry, &user)
if err != nil { if err != nil {
return err return err
} }