Merge pull request #8 from nostrband/develop
Add name saving to login flow, fix updateUI
This commit is contained in:
commit
1305af6896
@ -53,8 +53,16 @@ export const ModalLogin = () => {
|
|||||||
const submitHandler = async (values: FormInputType) => {
|
const submitHandler = async (values: FormInputType) => {
|
||||||
try {
|
try {
|
||||||
let npub = values.username
|
let npub = values.username
|
||||||
if (!npub.startsWith('npub1') && !npub.includes('@')) {
|
let name = ''
|
||||||
npub += '@' + DOMAIN
|
if (!npub.startsWith('npub1')) {
|
||||||
|
name = npub
|
||||||
|
if (!npub.includes('@')) {
|
||||||
|
npub += '@' + DOMAIN
|
||||||
|
} else {
|
||||||
|
const nameDomain = npub.split('@')
|
||||||
|
if (nameDomain[1] === DOMAIN)
|
||||||
|
name = nameDomain[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (npub.includes('@')) {
|
if (npub.includes('@')) {
|
||||||
const npubNip05 = await fetchNip05(npub)
|
const npubNip05 = await fetchNip05(npub)
|
||||||
@ -63,12 +71,13 @@ export const ModalLogin = () => {
|
|||||||
}
|
}
|
||||||
const passphrase = values.password
|
const passphrase = values.password
|
||||||
|
|
||||||
console.log('fetch', npub, passphrase)
|
console.log('fetch', npub, name)
|
||||||
const k: any = await swicCall('fetchKey', npub, passphrase)
|
const k: any = await swicCall('fetchKey', npub, passphrase, name)
|
||||||
notify(`Fetched ${k.npub}`, 'success')
|
notify(`Fetched ${k.npub}`, 'success')
|
||||||
cleanUpStates()
|
cleanUpStates()
|
||||||
navigate(`/key/${k.npub}`)
|
navigate(`/key/${k.npub}`)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.log("error", error);
|
||||||
notify(error?.message || 'Something went wrong!', 'error')
|
notify(error?.message || 'Something went wrong!', 'error')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,10 +160,12 @@ export class NoauthBackend {
|
|||||||
const self = this
|
const self = this
|
||||||
swg.addEventListener('activate', (event) => {
|
swg.addEventListener('activate', (event) => {
|
||||||
console.log('activate')
|
console.log('activate')
|
||||||
|
// swg.addEventListener('activate', event => event.waitUntil(swg.clients.claim()));
|
||||||
})
|
})
|
||||||
|
|
||||||
swg.addEventListener('install', (event) => {
|
swg.addEventListener('install', (event) => {
|
||||||
console.log('install')
|
console.log('install')
|
||||||
|
// swg.addEventListener('install', event => event.waitUntil(swg.skipWaiting()));
|
||||||
})
|
})
|
||||||
|
|
||||||
swg.addEventListener('push', (event) => {
|
swg.addEventListener('push', (event) => {
|
||||||
@ -521,7 +523,9 @@ export class NoauthBackend {
|
|||||||
return generatePrivateKey()
|
return generatePrivateKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async addKey(name: string, nsec?: string): Promise<KeyInfo> {
|
public async addKey({ name, nsec, existingName }
|
||||||
|
: { name: string, nsec?: string, existingName?: boolean }
|
||||||
|
): Promise<KeyInfo> {
|
||||||
|
|
||||||
// lowercase
|
// lowercase
|
||||||
name = name.trim().toLocaleLowerCase()
|
name = name.trim().toLocaleLowerCase()
|
||||||
@ -548,9 +552,10 @@ export class NoauthBackend {
|
|||||||
// assign nip05 before adding the key
|
// assign nip05 before adding the key
|
||||||
// FIXME set name to db and if this call to 'send' fails
|
// FIXME set name to db and if this call to 'send' fails
|
||||||
// then retry later
|
// then retry later
|
||||||
console.log("adding key", npub, name)
|
if (!existingName && name && !name.includes('@')) {
|
||||||
if (name)
|
console.log("adding key", npub, name)
|
||||||
await this.sendNameToServer(npub, name)
|
await this.sendNameToServer(npub, name)
|
||||||
|
}
|
||||||
|
|
||||||
const sub = await this.swg.registration.pushManager.getSubscription()
|
const sub = await this.swg.registration.pushManager.getSubscription()
|
||||||
if (sub) await this.sendSubscriptionToServer(npub, sub)
|
if (sub) await this.sendSubscriptionToServer(npub, sub)
|
||||||
@ -825,13 +830,13 @@ export class NoauthBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async generateKey(name: string) {
|
private async generateKey(name: string) {
|
||||||
const k = await this.addKey(name)
|
const k = await this.addKey({ name })
|
||||||
this.updateUI()
|
this.updateUI()
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
private async importKey(name: string, nsec: string) {
|
private async importKey(name: string, nsec: string) {
|
||||||
const k = await this.addKey(name, nsec)
|
const k = await this.addKey({ name, nsec })
|
||||||
this.updateUI()
|
this.updateUI()
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
@ -851,7 +856,7 @@ export class NoauthBackend {
|
|||||||
await this.sendKeyToServer(npub, enckey, pwh)
|
await this.sendKeyToServer(npub, enckey, pwh)
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fetchKey(npub: string, passphrase: string) {
|
private async fetchKey(npub: string, passphrase: string, name: string) {
|
||||||
const { type, data: pubkey } = nip19.decode(npub)
|
const { type, data: pubkey } = nip19.decode(npub)
|
||||||
if (type !== 'npub') throw new Error(`Invalid npub ${npub}`)
|
if (type !== 'npub') throw new Error(`Invalid npub ${npub}`)
|
||||||
const { pwh } = await this.keysModule.generatePassKey(
|
const { pwh } = await this.keysModule.generatePassKey(
|
||||||
@ -870,7 +875,7 @@ export class NoauthBackend {
|
|||||||
enckey,
|
enckey,
|
||||||
passphrase,
|
passphrase,
|
||||||
})
|
})
|
||||||
const k = await this.addKey(nsec)
|
const k = await this.addKey({ name, nsec, existingName: true })
|
||||||
this.updateUI()
|
this.updateUI()
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
@ -943,7 +948,7 @@ export class NoauthBackend {
|
|||||||
} else if (method === 'saveKey') {
|
} else if (method === 'saveKey') {
|
||||||
result = await this.saveKey(args[0], args[1])
|
result = await this.saveKey(args[0], args[1])
|
||||||
} else if (method === 'fetchKey') {
|
} else if (method === 'fetchKey') {
|
||||||
result = await this.fetchKey(args[0], args[1])
|
result = await this.fetchKey(args[0], args[1], args[2])
|
||||||
} else if (method === 'confirm') {
|
} else if (method === 'confirm') {
|
||||||
result = await this.confirm(args[0], args[1], args[2], args[3])
|
result = await this.confirm(args[0], args[1], args[2], args[3])
|
||||||
} else if (method === 'deleteApp') {
|
} else if (method === 'deleteApp') {
|
||||||
@ -969,7 +974,9 @@ export class NoauthBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async updateUI() {
|
private async updateUI() {
|
||||||
const clients = await this.swg.clients.matchAll()
|
const clients = await this.swg.clients.matchAll({
|
||||||
|
includeUncontrolled: true
|
||||||
|
})
|
||||||
console.log('updateUI clients', clients.length)
|
console.log('updateUI clients', clients.length)
|
||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
client.postMessage({})
|
client.postMessage({})
|
||||||
|
@ -17,7 +17,17 @@ export async function swicRegister() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
navigator.serviceWorker.ready.then((r) => (swr = r))
|
navigator.serviceWorker.ready.then((r) => {
|
||||||
|
console.log("sw ready")
|
||||||
|
swr = r
|
||||||
|
if (navigator.serviceWorker.controller) {
|
||||||
|
console.log(
|
||||||
|
`This page is currently controlled by: ${navigator.serviceWorker.controller}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.log("This page is not currently controlled by a service worker.");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
navigator.serviceWorker.addEventListener('message', (event) => {
|
navigator.serviceWorker.addEventListener('message', (event) => {
|
||||||
onMessage((event as MessageEvent).data)
|
onMessage((event as MessageEvent).data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user