Skip to content

Commit

Permalink
Merge pull request #543 from clrfund/gundb-create-error
Browse files Browse the repository at this point in the history
Detect gunDB user already being created
  • Loading branch information
auryn-macmillan authored Aug 25, 2022
2 parents 6ac8605 + e3a36a7 commit 5c3667b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
8 changes: 7 additions & 1 deletion vue-app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ export default class App extends Vue {
if (!this.$web3.user) return
// Connect & auth to gun db
await this.$store.dispatch(LOGIN_USER, this.$web3.user)
try {
await this.$store.dispatch(LOGIN_USER, this.$web3.user)
} catch (error) {
/* eslint-disable-next-line no-console */
console.error(error)
return
}
this.$store.commit(SET_CURRENT_USER, this.$web3.user)
this.$store.dispatch(LOAD_USER_INFO)
Expand Down
15 changes: 10 additions & 5 deletions vue-app/src/api/gun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { gunPeers } from './core'
import { LOGIN_MESSAGE } from './user'
import { md5 } from '@/utils/crypto'

const GUN_WARNINGS = new Set([
'User already created!',
'User is already being created or authenticated!',
])

interface GunSchema {
data: { [key: string]: string }
}
Expand All @@ -26,19 +31,19 @@ export async function loginUser(
const password = encryptionKey
await new Promise((resolve, reject) => {
user.create(username, password, (ack) => {
if (ack.ok === 0 || ack.err === 'User already created!') {
resolve()
if (ack.ok === 0 || GUN_WARNINGS.has(ack.err)) {
resolve(0)
} else {
reject()
reject('Error creating user in GunDB.')
}
})
})
await new Promise((resolve, reject) => {
user.auth(username, password, (ack) => {
if (ack.err) {
reject()
reject('Error authenticating user in GunDB.')
} else {
resolve()
resolve(0)
}
})
})
Expand Down

0 comments on commit 5c3667b

Please sign in to comment.