Skip to content

Commit 750b023

Browse files
fix: don't swallow errors in POST /accounts (#921)
1 parent e621573 commit 750b023

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/api/accounts/CreateAccount.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as Joi from 'joi'
22
import bytesToUuid = require('uuid/lib/bytesToUuid')
33

44
import { PasswordComplexConfiguration } from '../../api/PasswordComplexConfiguration'
5-
import { errors } from '../../errors/errors'
65
import { validatePassword } from '../../helpers/validatePassword'
76
import { AccountsController } from '../../modules/Accounts/Accounts.controller'
87
import { SendEmailTo } from '../../utils/SendEmail'
@@ -19,16 +18,8 @@ export const CreateAccountSchema = (
1918
export const CreateAccount = (sendEmail: SendEmailTo, verifiedAccount: boolean, pwnedCheckerRoot: string) => async (
2019
ctx: any,
2120
): Promise<any> => {
22-
const logger = ctx.logger(__dirname)
23-
24-
try {
25-
const usersController = new AccountsController(ctx.logger, verifiedAccount, pwnedCheckerRoot, sendEmail)
26-
const { email, password } = ctx.request.body
27-
const { account: { id, issuer }, token } = await usersController.create({ email, password })
28-
ctx.body = { id: bytesToUuid(id), issuer, token }
29-
} catch (exception) {
30-
const { AccountAlreadyExists } = errors
31-
logger.error({ exception }, 'api.CreateAccount')
32-
ctx.throw(AccountAlreadyExists.code, AccountAlreadyExists.message)
33-
}
21+
const usersController = new AccountsController(ctx.logger, verifiedAccount, pwnedCheckerRoot, sendEmail)
22+
const { email, password } = ctx.request.body
23+
const { account: { id, issuer }, token } = await usersController.create({ email, password })
24+
ctx.body = { id: bytesToUuid(id), issuer, token }
3425
}

src/modules/Accounts/Accounts.controller.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Pino from 'pino'
33

44
import { Token } from '../../api/Tokens'
55
import { getToken } from '../../api/accounts/utils/utils'
6+
import { AccountAlreadyExists } from '../../errors/errors'
67
import { uuid4 } from '../../helpers/uuid'
78
import { GenericDAO } from '../../interfaces/GenericDAO'
89
import { Network } from '../../interfaces/Network'
@@ -30,6 +31,11 @@ export class AccountsController {
3031
public async create({ email, password }: { email: string, password: string }) {
3132
this.logger.debug({ email }, 'Creating account')
3233

34+
const existing = await Account.findOne({ email })
35+
36+
if (existing)
37+
throw new AccountAlreadyExists()
38+
3339
const id = await this.getUnusedId()
3440
const { privateKey, publicKey } = generateED25519Base58Keys()
3541
const encryptedPrivateKey = await Vault.encrypt(privateKey)

0 commit comments

Comments
 (0)