Skip to content

Commit

Permalink
feat: auto link alby account (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz authored Jul 25, 2024
1 parent 7a92191 commit 5110230
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#RELAY=ws://localhost:7447/v1
#PORT=8080
#FRONTEND_URL=http://localhost:5173
#CONNECT_ALBY_ACCOUNT=false
#AUTO_LINK_ALBY_ACCOUNT=false

# set LDK_GOSSIP_SOURCE as empty to not use RGS
#LDK_GOSSIP_SOURCE=
Expand Down
11 changes: 10 additions & 1 deletion alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewAlbyOAuthService(db *gorm.DB, cfg config.Config, keys keys.Keys, eventPu
return albyOAuthSvc
}

func (svc *albyOAuthService) CallbackHandler(ctx context.Context, code string) error {
func (svc *albyOAuthService) CallbackHandler(ctx context.Context, code string, lnClient lnclient.LNClient) error {
token, err := svc.oauthConf.Exchange(ctx, code)
if err != nil {
logger.Logger.WithError(err).Error("Failed to exchange token")
Expand All @@ -95,6 +95,15 @@ func (svc *albyOAuthService) CallbackHandler(ctx context.Context, code string) e
// save the user's alby account ID on first time login
if existingUserIdentifier == "" {
svc.cfg.SetUpdate(userIdentifierKey, me.Identifier, "")

if svc.cfg.GetEnv().AutoLinkAlbyAccount {
// link account on first login
err := svc.LinkAccount(ctx, lnClient, 1_000_000, constants.BUDGET_RENEWAL_MONTHLY)
if err != nil {
logger.Logger.WithError(err).Error("Failed to link account on first auth callback")
}
}

} else if me.Identifier != existingUserIdentifier {
// remove token so user can retry with correct account
svc.cfg.SetUpdate(accessTokenKey, "", "")
Expand Down
2 changes: 1 addition & 1 deletion alby/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type AlbyOAuthService interface {
GetUserIdentifier() (string, error)
IsConnected(ctx context.Context) bool
LinkAccount(ctx context.Context, lnClient lnclient.LNClient, budget uint64, renewal string) error
CallbackHandler(ctx context.Context, code string) error
CallbackHandler(ctx context.Context, code string, lnClient lnclient.LNClient) error
GetBalance(ctx context.Context) (*AlbyBalance, error)
GetMe(ctx context.Context) (*AlbyMe, error)
SendPayment(ctx context.Context, invoice string) error
Expand Down
1 change: 1 addition & 0 deletions config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type AppConfig struct {
BaseUrl string `envconfig:"BASE_URL" default:"http://localhost:8080"`
FrontendUrl string `envconfig:"FRONTEND_URL"`
LogEvents bool `envconfig:"LOG_EVENTS" default:"false"`
AutoLinkAlbyAccount bool `envconfig:"AUTO_LINK_ALBY_ACCOUNT" default:"true"`
PhoenixdAddress string `envconfig:"PHOENIXD_ADDRESS"`
PhoenixdAuthorization string `envconfig:"PHOENIXD_AUTHORIZATION"`
GoProfilerAddr string `envconfig:"GO_PROFILER_ADDR"`
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/AuthCodeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function AuthCodeForm() {
<div className="grid gap-1.5">
<Label htmlFor="authorization-code">Authorization Code</Label>
<Input
type="text"
type="password"
name="authorization-code"
id="authorization-code"
placeholder="Enter code you see in the browser"
Expand Down
2 changes: 1 addition & 1 deletion http/alby_http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (albyHttpSvc *AlbyHttpService) RegisterSharedRoutes(e *echo.Echo, authMiddl
func (albyHttpSvc *AlbyHttpService) albyCallbackHandler(c echo.Context) error {
code := c.QueryParam("code")

err := albyHttpSvc.albyOAuthSvc.CallbackHandler(c.Request().Context(), code)
err := albyHttpSvc.albyOAuthSvc.CallbackHandler(c.Request().Context(), code, albyHttpSvc.svc.GetLNClient())
if err != nil {
logger.Logger.WithError(err).Error("Failed to handle Alby OAuth callback")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Expand Down
2 changes: 1 addition & 1 deletion wails/wails_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (app *WailsApp) WailsRequestRouter(route string, method string, body string
case len(authCodeMatch) > 1:
code := authCodeMatch[1]

err := app.svc.GetAlbyOAuthSvc().CallbackHandler(ctx, code)
err := app.svc.GetAlbyOAuthSvc().CallbackHandler(ctx, code, app.svc.GetLNClient())
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"route": route,
Expand Down

0 comments on commit 5110230

Please sign in to comment.