Skip to content

Commit

Permalink
fix: WebLN QR fallback for anon users
Browse files Browse the repository at this point in the history
  • Loading branch information
Soxasora committed Feb 3, 2025
1 parent 89187db commit 89da4b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 8 additions & 1 deletion components/use-paid-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ export function usePaidMutation (mutation,

const paymentAttempted = walletError instanceof WalletPaymentError
if (paymentAttempted) {
walletInvoice = await invoiceHelper.retry(walletInvoice, { update: updateOnFallback })
try {
walletInvoice = await invoiceHelper.retry(walletInvoice, { update: updateOnFallback })
} catch (err) {
if (err.message.includes('must be logged in')) {
// For anonymous users, skip retry and go straight to QR payment
console.log('anon detected - skipping retry, showing QR payment')
}
}
}
return await waitForQrPayment(walletInvoice, walletError, { persistOnNavigate, waitFor })
}, [waitForWalletPayment, waitForQrPayment, invoiceHelper])
Expand Down
8 changes: 6 additions & 2 deletions wallets/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ export function useWalletPayment () {
}

if (paymentError instanceof WalletPaymentError) {
// if a payment was attempted, cancel invoice to make sure it cannot be paid later and create new invoice to retry.
await invoiceHelper.cancel(latestInvoice)
// only cancel the invoice if we're not dealing with a WebLN error
// as WebLN failures need to fall back to QR code and anonymous users can't retry
if (paymentError.wallet !== 'webln') {
// if a payment was attempted, cancel invoice to make sure it cannot be paid later and create new invoice to retry.
await invoiceHelper.cancel(latestInvoice)
}
}

// only create a new invoice if we will try to pay with a wallet again
Expand Down
7 changes: 6 additions & 1 deletion wallets/webln/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'react'
import { SSR } from '@/lib/constants'
import { WalletError } from '../errors'
export * from '@/wallets/webln'

export const sendPayment = async (bolt11) => {
Expand All @@ -8,7 +9,11 @@ export const sendPayment = async (bolt11) => {
}

// this will prompt the user to unlock the wallet if it's locked
await window.webln.enable()
try {
await window.webln.enable()
} catch (err) {
throw new WalletError('cannot re-enable wallet')
}

// this will prompt for payment if no budget is set
const response = await window.webln.sendPayment(bolt11)
Expand Down

0 comments on commit 89da4b4

Please sign in to comment.