fix: use clipboard mode for Amber login in Capacitor APK

Capacitor native apps fail with "malformed nostrsigner request" when
using callbackUrl because Amber treats custom URI schemes as invalid.

Fix: detect window.Capacitor.isNativePlatform() and use the no-callback
clipboard flow (openAmberForLoginNoCallback) instead of the callbackUrl
flow. Amber copies the pubkey to clipboard, app reads it on
visibilitychange when user returns.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-06-18 22:56:28 +07:00
parent 3f654f4a35
commit 55856e2935

View file

@ -90,18 +90,16 @@ const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLogin, onS
setAmberStep('waiting');
setErrors(prev => ({ ...prev, amber: undefined }));
// Detect if running as installed PWA (standalone) — window.matchMedia
// returns true when launched from home screen on Android/iOS
// Detect if running as Capacitor native app or installed PWA (standalone)
const isCapacitor = !!(window as unknown as { Capacitor?: { isNativePlatform?: () => boolean } }).Capacitor?.isNativePlatform?.();
const isPWA = window.matchMedia('(display-mode: standalone)').matches
|| ('standalone' in window.navigator && (window.navigator as { standalone?: boolean }).standalone === true);
if (isPWA) {
// ── PWA / Capacitor mode ───────────────────────────────────────────────
// When installed to home screen or running as a Capacitor APK, the app
// is single-tab. Use openAmberForLogin() which handles Capacitor WebViews
// correctly via window.open(_system) instead of window.location.href.
// Amber copies the pubkey to clipboard when there's no callback.
// When the user comes back, visibilitychange fires and we read the clipboard.
if (isCapacitor || isPWA) {
// ── Capacitor APK / PWA mode ───────────────────────────────────────────
// In Capacitor, callbackUrl flows don't work — Amber treats the custom
// scheme as a malformed request. Use no-callback mode: Amber copies the
// pubkey to clipboard, we read it when the app regains focus.
openAmberForLoginNoCallback();
// When user returns to the PWA after approving in Amber, auto-read clipboard