feat: integrate nstart (#33)

This commit is contained in:
Cody Tseng
2025-01-29 15:32:26 +08:00
committed by GitHub
parent 7daa566cec
commit a264b747e7
15 changed files with 154 additions and 47 deletions

View File

@@ -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)