diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index f8fc73ec..533b57cb 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -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') {