diff --git a/.gitignore b/.gitignore index 717048a..effd990 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Dependencies node_modules/ -nostr-tools/ + # IDE and OS files .idea/ diff --git a/README.md b/README.md index 03d730e..9b9ec8e 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,47 @@ Nostr_Login_Lite =========== -## Floating Tab API +## API -Configure persistent floating tab for login/logout: +Configure for login/logout: ```javascript -await NOSTR_LOGIN_LITE.init({ - // Set the initial theme (default: 'default') - theme: 'dark', // Choose from 'default' or 'dark' - - // Standard configuration options +await window.NOSTR_LOGIN_LITE.init({ + theme: 'default', + methods: { extension: true, local: true, + seedphrase: true, // β Must be explicitly enabled readonly: true, connect: true, - otp: true + otp: false }, - // Floating tab configuration (now uses theme-aware text icons) floatingTab: { enabled: true, - hPosition: 0.95, // 0.0-1.0 or '95%' from left - vPosition: 0.5, // 0.0-1.0 or '50%' from top - getUserInfo: true, // Fetch user profile name from relays - getUserRelay: [ // Relays for profile queries - 'wss://relay.damus.io', - 'wss://nos.lol' - ], + hPosition: 0.95, // Near right edge + vPosition: 0.1, // Near top + appearance: { - style: 'pill', // 'pill', 'square', 'circle', 'minimal' - theme: 'auto', // 'auto' follows main theme - icon: '[LOGIN]', // Now uses text-based icons like [LOGIN], [KEY], [NET] - text: 'Login' + style: 'pill', + icon: '[LOGIN]', + text: 'Sign In', + iconOnly: false }, + behavior: { - hideWhenAuthenticated: false, + hideWhenAuthenticated: false, // Keep visible after login showUserInfo: true, autoSlide: true }, - animation: { - slideDirection: 'auto' // 'auto', 'left', 'right', 'up', 'down' - } + + getUserInfo: true, // β Fetch user profiles + getUserRelay: ['wss://relay.laantungir.net'] // Custom relays for profiles } }); + // After initialization, you can switch themes dynamically: NOSTR_LOGIN_LITE.switchTheme('dark'); NOSTR_LOGIN_LITE.switchTheme('default'); @@ -86,3 +82,31 @@ const modal = NOSTR_LOGIN_LITE.embed('#login-container', { ``` Container can be CSS selector or DOM element. Modal renders inline without backdrop overlay. + +## Logout API + +To log out users and clear authentication state: + +```javascript +// Unified logout method - works for all authentication methods +window.NOSTR_LOGIN_LITE.logout(); +``` + +This will: +- Clear persistent authentication data from localStorage +- Dispatch `nlLogout` event for custom cleanup +- Reset the authentication state across all components + +### Event Handling + +Listen for logout events in your application: + +```javascript +window.addEventListener('nlLogout', () => { + console.log('User logged out'); + // Clear your application's UI state + // Redirect to login page, etc. +}); +``` + +The logout system works consistently across all authentication methods (extension, local keys, NIP-46, etc.) and all UI components (floating tab, modal, embedded). diff --git a/examples/button.html b/examples/button.html index e0da565..d68664b 100644 --- a/examples/button.html +++ b/examples/button.html @@ -35,7 +35,7 @@ } #login-button:hover { - background: #0052a3; + opacity: 0.8; } @@ -49,6 +49,9 @@
+This tab uses sessionStorage - authentication is isolated to this window only. Refreshing will maintain your login state, but other tabs/windows are independent.
+diff --git a/examples/embedded.html b/examples/embedded.html index 3a7a87b..7e7c47f 100644 --- a/examples/embedded.html +++ b/examples/embedded.html @@ -41,6 +41,7 @@ methods: { extension: true, local: true, + seedphrase: true, readonly: true, connect: true, remote: true, diff --git a/examples/login-and-profile.html b/examples/login-and-profile.html index 349d656..242c161 100644 --- a/examples/login-and-profile.html +++ b/examples/login-and-profile.html @@ -51,19 +51,20 @@ await window.NOSTR_LOGIN_LITE.init({ theme: 'default', darkMode: false, - relays: [relayUrl, 'wss://relay.damus.io'], methods: { extension: true, local: true, - readonly: true, + seedphrase: true, connect: true, // Enables "Nostr Connect" (NIP-46) remote: true, // Also needed for "Nostr Connect" compatibility otp: true // Enables "DM/OTP" }, floatingTab: { enabled: true, - hPosition: 0.80, // 95% from left - vPosition: 0.01, // 50% from top (center) + hPosition: .98, // 95% from left + vPosition: 0, // 50% from top (center) + getUserInfo: true, // Fetch user profiles + getUserRelay: ['wss://relay.laantungir.net'], // Custom relays for profiles appearance: { style: 'minimal', theme: 'auto', @@ -88,16 +89,17 @@ console.log('SUCCESS', 'NOSTR_LOGIN_LITE initialized successfully'); window.addEventListener('nlMethodSelected', handleAuthEvent); + window.addEventListener('nlLogout', handleLogoutEvent); } catch (error) { console.log('ERROR', `Initialization failed: ${error.message}`); - + } } function handleAuthEvent(event) { - const {pubkey, method, error } = event.detail; + const { pubkey, method, error } = event.detail; console.log('INFO', `Auth event received: method=${method}`); if (method && pubkey) { @@ -108,10 +110,20 @@ } else if (error) { console.log('ERROR', `Authentication error: ${error}`); - + } } + function handleLogoutEvent() { + console.log('INFO', 'Logout event received'); + // Clear local UI state + userPubkey = null; + document.getElementById('profile-name').textContent = ''; + document.getElementById('profile-about').textContent = ''; + document.getElementById('profile-pubkey').textContent = ''; + document.getElementById('profile-picture').src = ''; + } + // Load user profile using nostr-tools pool async function loadUserProfile() { if (!userPubkey) return; @@ -124,7 +136,7 @@ // Create a SimplePool instance const pool = new window.NostrTools.SimplePool(); const relays = [relayUrl, 'wss://relay.laantungir.net']; - + // Get profile event (kind 0) for the user using querySync const events = await pool.querySync(relays, { kinds: [0], @@ -171,7 +183,7 @@ async function logout() { console.log('INFO', 'Logging out...'); try { - await nlLite.logout(); + window.NOSTR_LOGIN_LITE.logout(); console.log('SUCCESS', 'Logged out successfully'); } catch (error) { console.log('ERROR', `Logout failed: ${error.message}`); diff --git a/examples/modal.html b/examples/modal.html index cb01a6e..639f2f2 100644 --- a/examples/modal.html +++ b/examples/modal.html @@ -41,6 +41,7 @@ methods: { extension: true, local: true, + seedphrase:true, readonly: true, connect: true, remote: true, diff --git a/examples/session-isolation-test.html b/examples/session-isolation-test.html new file mode 100644 index 0000000..58cc2e2 --- /dev/null +++ b/examples/session-isolation-test.html @@ -0,0 +1,534 @@ + + +
+ + +
+ + +
+