Files
einundzwanzig-verein/resources/js/nostrLogin.js
fsociety 1d5079bfa0 feat: handle logout events in nostr-login
This commit introduces handling for logout events in nostr-login across various pages. When a user logs out, the current public key and other related information are reset to null. The nostrLogin.js file has also been updated to dispatch a 'nostrLoggedOut' event when this occurs. Additionally, the nostr-login package has been added to the project dependencies.
2024-10-06 13:38:49 +02:00

49 lines
1.9 KiB
JavaScript

import {init as initNostrLogin} from 'nostr-login';
export default () => ({
openNostrLogin() {
window.nostr.getPublicKey();
},
async init() {
await initNostrLogin({
methods: ['connect', 'extension'],
onAuth: async (npub, options) => {
console.log('User logged in', npub, options);
console.log('type', options.method);
if (options.method === 'readOnly') {
console.log('User logged in as read-only');
return;
}
if (options.method === undefined) {
Alpine.store('nostr', {user: null});
this.$dispatch('nostrLoggedOut', {});
return;
}
const pubkey = await window.nostr.getPublicKey();
// fetch profile from /api/nostr/profile/{publicKey}
fetch('/api/nostr/profile/' + pubkey)
.then(response => response.json())
.then(data => {
console.log('Profile fetched', data);
// store the profile in AlpineJS store
Alpine.store('nostr', {user: data});
this.$dispatch('nostrLoggedIn', {pubkey: data.pubkey});
document.addEventListener('nlAuth', (e) => {
// type is login, signup or logout
if (e.detail.type === 'login' || e.detail.type === 'signup') {
} else {
console.log('User logged out')
Alpine.store('nostr', {user: null});
this.$dispatch('nostrLoggedOut', {});
}
})
});
}
});
},
});