feat: integrate nstart (#33)
This commit is contained in:
@@ -69,6 +69,10 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
if (hasNostrLoginHash()) {
|
||||
return await loginByNostrLoginHash()
|
||||
}
|
||||
|
||||
const accounts = storage.getAccounts()
|
||||
const act = storage.getCurrentAccount() ?? accounts[0] // auto login the first account
|
||||
if (!act) return
|
||||
@@ -76,6 +80,18 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
await loginWithAccountPointer(act)
|
||||
}
|
||||
init()
|
||||
|
||||
const handleHashChange = () => {
|
||||
if (hasNostrLoginHash()) {
|
||||
loginByNostrLoginHash()
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('hashchange', handleHashChange)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('hashchange', handleHashChange)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -138,6 +154,24 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
})
|
||||
}, [account])
|
||||
|
||||
const hasNostrLoginHash = () => {
|
||||
return window.location.hash && window.location.hash.startsWith('#nostr-login')
|
||||
}
|
||||
|
||||
const loginByNostrLoginHash = async () => {
|
||||
const credential = window.location.hash.replace('#nostr-login=', '')
|
||||
const urlWithoutHash = window.location.href.split('#')[0]
|
||||
history.replaceState(null, '', urlWithoutHash)
|
||||
|
||||
if (credential.startsWith('bunker://')) {
|
||||
return await bunkerLogin(credential)
|
||||
} else if (credential.startsWith('ncryptsec')) {
|
||||
return await ncryptsecLogin(credential)
|
||||
} else if (credential.startsWith('nsec')) {
|
||||
return await nsecLogin(credential)
|
||||
}
|
||||
}
|
||||
|
||||
const login = (signer: ISigner, act: TAccount) => {
|
||||
storage.addAccount(act)
|
||||
storage.switchAccount(act)
|
||||
|
||||
Reference in New Issue
Block a user