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.
This commit is contained in:
fsociety
2024-10-06 13:38:49 +02:00
parent a7716152d4
commit 1d5079bfa0
9 changed files with 134 additions and 29 deletions

View File

@@ -15,6 +15,7 @@
"flatpickr": "^4.6.13",
"laravel-echo": "^1.16.1",
"laravel-vite-plugin": "^1.0",
"nostr-login": "^1.6.6",
"nostr-tools": "^2.7.2",
"postcss": "^8.4.41",
"pusher-js": "^8.4.0-rc2",

View File

@@ -1,29 +1,48 @@
import {init as initNostrLogin} from 'nostr-login';
export default () => ({
openNostrLogin() {
window.nostr.getPublicKey();
},
init() {
// listen for nostr auth events
document.addEventListener('nlAuth', (e) => {
// type is login, signup or logout
if (e.detail.type === 'login' || e.detail.type === 'signup') {
console.log('User logged in');
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/' + e.detail.pubkey)
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 });
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', {});
}
})
});
} else {
console.log('User logged out')
Alpine.store('nostr', { user: null });
}
})
});
},
});

View File

@@ -9,8 +9,6 @@
@vite(['resources/js/app.js','resources/css/app.css'])
@googlefonts
<script src="https://kit.fontawesome.com/866fd3d0ab.js" crossorigin="anonymous"></script>
<script src='https://www.unpkg.com/nostr-login@latest/dist/unpkg.js' data-perms="sign_event:1,sign_event:0"
data-theme="default" data-dark-mode="true"></script>
@wireUiScripts
@stack('scripts')
</head>

View File

@@ -46,6 +46,14 @@ mount(function () {
}
});
on([
'nostrLoggedOut' => function () {
$this->isAllowed = false;
$this->currentPubkey = null;
$this->currentPleb = null;
},
]);
on([
'nostrLoggedIn' => function ($pubkey) {
$this->currentPubkey = $pubkey;

View File

@@ -59,6 +59,13 @@ mount(fn()
$this->loadBoardVotes(),
]);
on([
'nostrLoggedOut' => function () {
$this->currentPubkey = null;
$this->currentPleb = null;
},
]);
on([
'nostrLoggedIn' => fn($pubkey)
=> [

View File

@@ -23,6 +23,14 @@ mount(function () {
->toArray();
});
on([
'nostrLoggedOut' => function () {
$this->isAllowed = false;
$this->currentPubkey = null;
$this->currentPleb = null;
},
]);
on([
'nostrLoggedIn' => function ($pubkey) {
$this->currentPubkey = $pubkey;

View File

@@ -17,6 +17,13 @@ state(['isAllowed' => false]);
state(['currentPubkey' => null]);
state(['members' => []]);
on([
'nostrLoggedOut' => function () {
$this->isAllowed = false;
$this->currentPubkey = null;
},
]);
on([
'nostrLoggedIn' => function ($pubkey) {
$this->currentPubkey = $pubkey;
@@ -24,7 +31,7 @@ on([
->where('pubkey', $pubkey)->first();
$allowedPubkeys = [
'0adf67475ccc5ca456fd3022e46f5d526eb0af6284bf85494c0dd7847f3e5033',
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279'
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279',
];
if (!in_array($this->currentPubkey, $allowedPubkeys, true)) {
return redirect()->route('association.profile');

View File

@@ -48,6 +48,20 @@ updated([
},
]);
on([
'nostrLoggedOut' => function () {
$this->currentPubkey = null;
$this->currentPleb = null;
$this->yearsPaid = [];
$this->events = [];
$this->payments = [];
$this->invoice = null;
$this->qrCode = null;
$this->amountToPay = config('app.env') === 'production' ? 21000 : 1;
$this->currentYearIsPaid = false;
},
]);
on([
'nostrLoggedIn' => function ($pubkey) {
$this->currentPubkey = $pubkey;

View File

@@ -171,11 +171,23 @@
resolved "https://registry.yarnpkg.com/@kurkle/color/-/color-0.3.2.tgz#5acd38242e8bde4f9986e7913c8fdf49d3aa199f"
integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==
"@noble/ciphers@0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.2.0.tgz#a12cda60f3cf1ab5d7c77068c3711d2366649ed7"
integrity sha512-6YBxJDAapHSdd3bLDv6x2wRPwq4QFMUaB3HvljNBUTThDd12eSm7/3F+2lnfzx2jvM+S6Nsy0jEt9QbPqSwqRw==
"@noble/ciphers@^0.5.1":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.5.3.tgz#48b536311587125e0d0c1535f73ec8375cd76b23"
integrity sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==
"@noble/curves@1.1.0", "@noble/curves@~1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"
"@noble/curves@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
@@ -183,20 +195,13 @@
dependencies:
"@noble/hashes" "1.3.2"
"@noble/curves@^1.4.0":
"@noble/curves@^1.4.0", "@noble/curves@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.6.0.tgz#be5296ebcd5a1730fccea4786d420f87abfeb40b"
integrity sha512-TlaHRXDehJuRNR9TfZDNQ45mMEd5dwUwmicsafcIX4SsNiqnCHKjE/1alYPd/lDRVhxdhUAlv8uEhMCI5zjIJQ==
dependencies:
"@noble/hashes" "1.5.0"
"@noble/curves@~1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"
"@noble/hashes@1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
@@ -207,7 +212,7 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==
"@noble/hashes@1.5.0", "@noble/hashes@^1.3.1":
"@noble/hashes@1.5.0", "@noble/hashes@^1.3.1", "@noble/hashes@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.5.0.tgz#abadc5ca20332db2b1b2aa3e496e9af1213570b0"
integrity sha512-1j6kQFb7QRru7eKN3ZDvRcP13rugwdxZqCjbiAVZfIJwgj2A65UmT4TgARXGlXgnRkORLTDTrO19ZErt7+QXgA==
@@ -217,7 +222,7 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.3.tgz#39908da56a4adc270147bb07968bf3b16cfe1699"
integrity sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==
"@noble/secp256k1@^2.0.0":
"@noble/secp256k1@^2.0.0", "@noble/secp256k1@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-2.1.0.tgz#71d829a0b8ce84aea137708f82273977eea84597"
integrity sha512-XLEQQNdablO0XZOIniFQimiXsZDNwaYgL96dZwC54Q30imSbAOFf3NKtepc+cXyuZf5Q1HCgbqgZ2UFFuHVcEw==
@@ -261,6 +266,23 @@
utf8-buffer "^1.0.0"
websocket-polyfill "^0.0.3"
"@nostr-dev-kit/ndk@^2.3.1":
version "2.10.1"
resolved "https://registry.yarnpkg.com/@nostr-dev-kit/ndk/-/ndk-2.10.1.tgz#6c5aae304c81ca8515141749c906f43da436a943"
integrity sha512-DE4n+wLL8nzja9rwBC1yuDG+fE5aky/ByWQu9H+b8qsq5/iPokFaB87TUbdTMwSzP45qJTTRNVH95NsuXaoqig==
dependencies:
"@noble/curves" "^1.6.0"
"@noble/hashes" "^1.5.0"
"@noble/secp256k1" "^2.1.0"
"@scure/base" "^1.1.9"
debug "^4.3.6"
light-bolt11-decoder "^3.2.0"
nostr-tools "^2.7.1"
tseep "^1.2.2"
typescript-lru-cache "^2.0.0"
utf8-buffer "^1.0.0"
websocket-polyfill "^0.0.3"
"@pkgjs/parseargs@^0.11.0":
version "0.11.0"
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
@@ -351,7 +373,7 @@
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==
"@scure/base@^1.1.1", "@scure/base@~1.1.0":
"@scure/base@^1.1.1", "@scure/base@^1.1.9", "@scure/base@~1.1.0":
version "1.1.9"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.9.tgz#e5e142fbbfe251091f9c5f1dd4c834ac04c3dbd1"
integrity sha512-8YKhl8GHiNI/pU2VMaofa2Tor7PJRAjwQLBBuilkJ9L5+13yVbC7JO/wS7piioAvPSwR3JKM1IJ/u4xQzbcXKg==
@@ -590,7 +612,7 @@ debug@^2.2.0:
dependencies:
ms "2.0.0"
debug@^4.3.4:
debug@^4.3.4, debug@^4.3.6:
version "4.3.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52"
integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==
@@ -887,7 +909,7 @@ laravel-vite-plugin@^1.0:
picocolors "^1.0.0"
vite-plugin-full-reload "^1.1.0"
light-bolt11-decoder@^3.0.0:
light-bolt11-decoder@^3.0.0, light-bolt11-decoder@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/light-bolt11-decoder/-/light-bolt11-decoder-3.2.0.tgz#2d48f78386cde526c4131db8f9dfd3250a2d5d4d"
integrity sha512-3QEofgiBOP4Ehs9BI+RkZdXZNtSys0nsJ6fyGeSiAGCBsMwHGUDS/JQlY/sTnWs91A2Nh0S9XXfA8Sy9g6QpuQ==
@@ -1007,6 +1029,27 @@ normalize-range@^0.1.2:
resolved "https://registry.yarnpkg.com/normalize-range/-/normalize-range-0.1.2.tgz#2d10c06bdfd312ea9777695a4d28439456b75942"
integrity sha512-bdok/XvKII3nUpklnV6P2hxtMNrCboOjAcyBuQnWEhO665FwrSNRxU+AqpsyvO6LgGYPspN+lu5CLtw4jPRKNA==
nostr-login@^1.6.6:
version "1.6.6"
resolved "https://registry.yarnpkg.com/nostr-login/-/nostr-login-1.6.6.tgz#a3af07b4b7179c31c40ab432ea06f0ccca965fb2"
integrity sha512-XOpB9nG3Qgt7iea7gA1zn4TaTfUKCKGdCHKwErqLPtMk/q1Rhkzj5cq/66iU0WqC6mSiwENfTy1p4qaM7HzMtg==
dependencies:
"@nostr-dev-kit/ndk" "^2.3.1"
nostr-tools "^1.17.0"
tseep "^1.2.1"
nostr-tools@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.17.0.tgz#b6f62e32fedfd9e68ec0a7ce57f74c44fc768e8c"
integrity sha512-LZmR8GEWKZeElbFV5Xte75dOeE9EFUW/QLI1Ncn3JKn0kFddDKEfBbFN8Mu4TMs+L4HR/WTPha2l+PPuRnJcMw==
dependencies:
"@noble/ciphers" "0.2.0"
"@noble/curves" "1.1.0"
"@noble/hashes" "1.3.1"
"@scure/base" "1.1.1"
"@scure/bip32" "1.3.1"
"@scure/bip39" "1.2.1"
nostr-tools@^2.7.1, nostr-tools@^2.7.2:
version "2.7.2"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-2.7.2.tgz#74a6ff543a81da1dcce9563b9317faa17221acce"
@@ -1346,7 +1389,7 @@ ts-interface-checker@^0.1.9:
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==
tseep@^1.1.1:
tseep@^1.1.1, tseep@^1.2.1, tseep@^1.2.2:
version "1.3.1"
resolved "https://registry.yarnpkg.com/tseep/-/tseep-1.3.1.tgz#734c5f7ca37cb8af4e4e0a5c205742673562a10e"
integrity sha512-ZPtfk1tQnZVyr7BPtbJ93qaAh2lZuIOpTMjhrYa4XctT8xe7t4SAW9LIxrySDuYMsfNNayE51E/WNGrNVgVicQ==