Add Nostr login info to context

This commit is contained in:
Alex Gleason 2025-04-17 18:46:08 -05:00
parent b8175a459e
commit 9a95cbe5ce
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 32 additions and 1 deletions

View File

@ -150,6 +150,30 @@ export function MyComponent() {
The `useCurrentUser` hook should be used to ensure that the user is logged in before they are able to publish Nostr events.
### Nostr Login
Nostr supports several types of logins:
1. Login with nsec
2. Login with browser extension
3. Login with bunker URI
Functions to log in with each of these methods are exposed by the `useLoginActions` hook:
```tsx
function MyComponent() {
const login = useLoginActions();
login.nsec(nsec); // login by the user pasting their secret key
login.bunker(uri); // login by the user pasting a bunker URI
login.extension(); // login with a NIP-07 browser extension
return (
<div>{/* ... */}</div>
);
}
```
## Development Practices
- Uses React Query for data fetching and caching

View File

@ -5,7 +5,7 @@ import { NLogin, useNostrLogin } from '@nostrify/react/login';
export function useLoginActions() {
const { nostr } = useNostr();
const { addLogin } = useNostrLogin();
const { logins, addLogin, removeLogin } = useNostrLogin();
return {
// Login with a Nostr secret key
@ -23,5 +23,12 @@ export function useLoginActions() {
const login = await NLogin.fromExtension();
addLogin(login);
},
// Log out the current user
async logout(): Promise<void> {
const login = logins[0];
if (login) {
removeLogin(login.id);
}
}
};
}