mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 08:47:23 +00:00
Switch to the newly added account when logging in
addLogin appends to the end of the logins array, but the current user
is logins[0]. So when an already-logged-in user opened "Add another
account" and chose any login method, the new account silently landed
at the back of the queue while the prior account remained current.
Most damaging on the signup path: login.nsec(nsec) was called before
the profile step, then publishEvent({ kind: 0, ... }) signed kind 0
metadata with the *previous* user's signer — overwriting their profile.
- useLoginActions: wrap each addLogin in addAndActivate, which also
calls setLogin(login.id) to promote the new login to logins[0].
- AuthDialog.finishSignup: defensive guard that verifies the active
signer matches the just-generated nsec before publishing kind 0;
refuse and toast otherwise. Belt and suspenders against any future
regression in the switch logic.
- Add a targeted regression test for the auto-switch behavior.
This commit is contained in:
3 files changed
+94
-5
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import {
|
||||
NLogin,
|
||||
type NLoginType,
|
||||
type NostrConnectParams,
|
||||
type NostrConnectStatus,
|
||||
useNostrLogin,
|
||||
@@ -15,24 +16,34 @@ export { generateNostrConnectParams, generateNostrConnectURI } from '@nostrify/r
|
||||
|
||||
export function useLoginActions() {
|
||||
const { nostr } = useNostr();
|
||||
const { logins, addLogin, removeLogin } = useNostrLogin();
|
||||
const { logins, addLogin, setLogin, removeLogin } = useNostrLogin();
|
||||
const { config } = useAppContext();
|
||||
|
||||
// Add a login and promote it to be the current user. Without the
|
||||
// setLogin call the new login is appended to the end of the array,
|
||||
// leaving the prior account as logins[0] — which is what
|
||||
// useCurrentUser / useLoggedInAccounts treat as the active user.
|
||||
// Promoting here makes "Add another account" actually switch.
|
||||
const addAndActivate = (login: NLoginType) => {
|
||||
addLogin(login);
|
||||
setLogin(login.id);
|
||||
};
|
||||
|
||||
return {
|
||||
// Login with a Nostr secret key
|
||||
nsec(nsec: string): void {
|
||||
const login = NLogin.fromNsec(nsec);
|
||||
addLogin(login);
|
||||
addAndActivate(login);
|
||||
},
|
||||
// Login with a NIP-46 "bunker://" URI
|
||||
async bunker(uri: string): Promise<void> {
|
||||
const login = await NLogin.fromBunker(uri, nostr);
|
||||
addLogin(login);
|
||||
addAndActivate(login);
|
||||
},
|
||||
// Login with a NIP-07 browser extension
|
||||
async extension(): Promise<void> {
|
||||
const login = await NLogin.fromExtension();
|
||||
addLogin(login);
|
||||
addAndActivate(login);
|
||||
},
|
||||
// Login via nostrconnect:// (client-initiated NIP-46)
|
||||
// The client displays a QR code and waits for the remote signer to connect.
|
||||
@@ -45,7 +56,7 @@ export function useLoginActions() {
|
||||
onStatus?: (status: NostrConnectStatus) => void,
|
||||
): Promise<void> {
|
||||
const login = await NLogin.fromNostrConnect(params, nostr, { signal, onStatus });
|
||||
addLogin(login);
|
||||
addAndActivate(login);
|
||||
},
|
||||
// Get the relay URLs for NIP-46 nostrconnect communication
|
||||
getRelayUrls(): string[] {
|
||||
|
||||
Reference in new issue
Block a user