mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 14:14:47 +01:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/getAlby/lndhub.go/lib/responses"
|
|
"github.com/getAlby/lndhub.go/lib/service"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// AuthController : AuthController struct
|
|
type AuthController struct {
|
|
svc *service.LndhubService
|
|
}
|
|
|
|
func NewAuthController(svc *service.LndhubService) *AuthController {
|
|
return &AuthController{
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
type AuthRequestBody struct {
|
|
Login string `json:"login"`
|
|
Password string `json:"password"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
}
|
|
type AuthResponseBody struct {
|
|
RefreshToken string `json:"refresh_token"`
|
|
AccessToken string `json:"access_token"`
|
|
}
|
|
|
|
// Auth : Auth Controller
|
|
func (controller *AuthController) Auth(c echo.Context) error {
|
|
|
|
var body AuthRequestBody
|
|
|
|
if err := c.Bind(&body); err != nil {
|
|
c.Logger().Errorf("Failed to load auth user request body: %v", err)
|
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
|
}
|
|
if err := c.Validate(&body); err != nil {
|
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
|
}
|
|
|
|
accessToken, refreshToken, err := controller.svc.GenerateToken(c.Request().Context(), body.Login, body.Password, body.RefreshToken)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, responses.BadAuthError)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, &AuthResponseBody{
|
|
RefreshToken: refreshToken,
|
|
AccessToken: accessToken,
|
|
})
|
|
}
|