Skip to content

Commit e9be7e9

Browse files
committed
feat: Signer detection on page load + invoke only on click
🔧 SIGNER DETECTION FLOW: - Detect signer availability on page load (no popup) - Show 'Signer Detected!' message if extension is available - Only invoke signer when user clicks 'Sign In with Nostr' - Clear visual feedback with green success message ✅ RESULT: - Page load: Shows signer status without popup - User clicks: Invokes signer for authentication - Better UX with immediate status feedback - No unwanted popups on page load
1 parent bc0e7a9 commit e9be7e9

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/app/signin/page.tsx

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
'use client';
22

3-
import { useState } from 'react';
3+
import { useState, useEffect } from 'react';
44
import { useRouter } from 'next/navigation';
55
import { useNostrSigner } from '@/hooks/useNostrSigner';
66
import { useAuthStore } from '@/stores/useAuthStore';
77
import { logger } from '@/services/core/LoggingService';
88

99
export default function SigninPage() {
1010
const router = useRouter();
11-
const { isAvailable, isLoading, signer, detectSignerOnDemand, error } = useNostrSigner();
11+
const { isAvailable, isLoading, signer, error } = useNostrSigner();
1212
const { setUser, setAuthenticated } = useAuthStore();
1313
const [isSigningIn, setIsSigningIn] = useState(false);
1414
const [signinError, setSigninError] = useState<string | null>(null);
15-
16-
// Don't detect signer automatically - only when user clicks sign in
15+
const [signerDetected, setSignerDetected] = useState(false);
16+
17+
// Detect signer on page load to show status
18+
useEffect(() => {
19+
const checkSigner = () => {
20+
const hasSigner = typeof window !== 'undefined' && !!window.nostr;
21+
setSignerDetected(hasSigner);
22+
logger.info('Signer check on page load', { hasSigner });
23+
};
24+
25+
checkSigner();
26+
}, []);
1727

1828
const handleSignIn = async () => {
19-
console.log('Sign In button clicked - detecting signer...');
20-
21-
// Detect signer when user clicks sign in
22-
await detectSignerOnDemand();
29+
console.log('Sign In button clicked - invoking signer...');
2330

2431
if (!isAvailable || !signer) {
32+
logger.warn('No signer available', { isAvailable, hasSigner: !!signer });
2533
setSigninError('No Nostr signer available. Please install a Nostr browser extension.');
2634
return;
2735
}
2836

37+
logger.info('Invoking signer for authentication...');
38+
2939
setIsSigningIn(true);
3040
setSigninError(null);
3141

@@ -111,7 +121,7 @@ export default function SigninPage() {
111121
</p>
112122
</div>
113123

114-
{!isAvailable && !isLoading && (
124+
{!signerDetected && !isLoading && (
115125
<div className="mb-6 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
116126
<h3 className="font-semibold text-yellow-800 mb-2">Nostr Extension Required</h3>
117127
<p className="text-yellow-700 text-sm mb-3">
@@ -128,6 +138,20 @@ export default function SigninPage() {
128138
</div>
129139
)}
130140

141+
{signerDetected && (
142+
<div className="mb-6 p-4 bg-green-50 border border-green-200 rounded-lg">
143+
<div className="flex items-center">
144+
<svg className="w-5 h-5 text-green-600 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
145+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
146+
</svg>
147+
<h3 className="font-semibold text-green-800">Signer Detected!</h3>
148+
</div>
149+
<p className="text-green-700 text-sm mt-1">
150+
Your Nostr extension is ready. Click &quot;Sign In with Nostr&quot; to continue.
151+
</p>
152+
</div>
153+
)}
154+
131155
{isLoading && (
132156
<div className="text-center py-8">
133157
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600 mx-auto mb-4"></div>

src/hooks/useNostrSigner.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ export const useNostrSigner = () => {
5959

6060
// Only detect signer when explicitly requested (e.g., on signin page)
6161
const detectSignerOnDemand = useCallback(async () => {
62+
logger.info('Starting detectSignerOnDemand...', {
63+
service: 'useNostrSigner',
64+
method: 'detectSignerOnDemand'
65+
});
66+
6267
try {
6368
setLoading(true);
6469
setError(null);
6570

6671
// Simple check for window.nostr
6772
const hasSigner = typeof window !== 'undefined' && !!window.nostr;
73+
logger.info('Window.nostr check result', {
74+
hasSigner,
75+
hasWindow: typeof window !== 'undefined',
76+
nostrType: typeof window !== 'undefined' ? typeof window.nostr : 'undefined'
77+
});
78+
6879
setSignerAvailable(hasSigner);
6980

7081
if (hasSigner) {

0 commit comments

Comments
 (0)