avoid duplicate alias sign up

This commit is contained in:
Daniel Lugo 2020-07-15 12:01:38 -04:00
parent 601ca0e831
commit fee393ae83

View file

@ -1275,6 +1275,38 @@ const register = async (alias, pass) => {
)
}
// this import is done here to avoid circular dependency hell
const { timeout5 } = require('../contact-api/utils')
let userData = await timeout5(
new Promise(res => {
gun.get(`~@${alias}`).once(ud => res(ud))
})
)
if (userData) {
throw new Error(
'The given alias has been used before, use an unique alias instead.'
)
}
await new Promise(res => setTimeout(res, 300))
userData = await timeout5(
new Promise(res => {
gun.get(`~@${alias}`).once(ud => res(ud), {
// https://github.com/amark/gun/pull/971#issue-438630761
wait: 1500
})
})
)
if (userData) {
throw new Error(
'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)'
)
}
_isRegistering = true
/** @type {import('../contact-api/SimpleGUN').CreateAck} */
@ -1282,6 +1314,13 @@ const register = async (alias, pass) => {
user.create(alias, pass, ack => res(ack))
)
// An empty ack object seems to be caused by a duplicate alias sign up
if ('{}' === JSON.stringify(ack)) {
throw new Error(
'The given alias has been used before, use an unique alias instead. (Empty ack)'
)
}
_isRegistering = false
if (typeof ack.err === 'string') {