diff --git a/packages/worker/src/api/controllers/admin/auth.js b/packages/worker/src/api/controllers/admin/auth.js index c565974d28..386b1f4a88 100644 --- a/packages/worker/src/api/controllers/admin/auth.js +++ b/packages/worker/src/api/controllers/admin/auth.js @@ -47,7 +47,7 @@ exports.reset = async ctx => { const { email } = ctx.request.body const configured = await isEmailConfigured() if (!configured) { - throw "Please contact your platform administrator, SMTP is not configured." + ctx.throw(400, "Please contact your platform administrator, SMTP is not configured.") } try { const user = await getGlobalUserByEmail(email) @@ -65,16 +65,17 @@ exports.reset = async ctx => { */ exports.resetUpdate = async ctx => { const { resetCode, password } = ctx.request.body - const userId = await checkResetPasswordCode(resetCode) - if (!userId) { - throw "Cannot reset password." - } - const db = new CouchDB(GLOBAL_DB) - const user = await db.get(userId) - user.password = await hash(password) - await db.put(user) - ctx.body = { - message: "password reset successfully.", + try { + const userId = await checkResetPasswordCode(resetCode) + const db = new CouchDB(GLOBAL_DB) + const user = await db.get(userId) + user.password = await hash(password) + await db.put(user) + ctx.body = { + message: "password reset successfully.", + } + } catch (err) { + ctx.throw(400, "Cannot reset password.") } } diff --git a/packages/worker/src/api/controllers/admin/users.js b/packages/worker/src/api/controllers/admin/users.js index d141ca88e9..48a20141a3 100644 --- a/packages/worker/src/api/controllers/admin/users.js +++ b/packages/worker/src/api/controllers/admin/users.js @@ -5,8 +5,9 @@ const { StaticDatabases, } = require("@budibase/auth").db const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils -const { UserStatus } = require("../../../constants") -const { checkResetPasswordCode, checkInviteCode } = require("../../../utilities/redis") +const { UserStatus, EmailTemplatePurpose } = require("../../../constants") +const { checkInviteCode } = require("../../../utilities/redis") +const { sendEmail } = require("../../../utilities/email") const FIRST_USER_EMAIL = "test@test.com" const FIRST_USER_PASSWORD = "test" @@ -124,18 +125,27 @@ exports.find = async ctx => { } exports.invite = async ctx => { - + const { email } = ctx.request.body + const existing = await getGlobalUserByEmail(FIRST_USER_EMAIL) + if (existing) { + ctx.throw(400, "Email address already in use.") + } + await sendEmail(email, EmailTemplatePurpose.INVITATION) + ctx.body = { + message: "Invitation has been sent." + } } exports.inviteAccept = async ctx => { const { inviteCode } = ctx.request.body - const email = await checkInviteCode(inviteCode) - if (!email) { - throw "Unable to create new user, invitation invalid." + try { + const email = await checkInviteCode(inviteCode) + // redirect the request + delete ctx.request.body.inviteCode + ctx.request.body.email = email + // this will flesh out the body response + await exports.save(ctx) + } catch (err) { + ctx.throw(400, "Unable to create new user, invitation invalid.") } - // redirect the request - delete ctx.request.body.inviteCode - ctx.request.body.email = email - // this will flesh out the body response - await exports.save(ctx) } diff --git a/packages/worker/src/utilities/email.js b/packages/worker/src/utilities/email.js index 745abdf7a2..f7b9284402 100644 --- a/packages/worker/src/utilities/email.js +++ b/packages/worker/src/utilities/email.js @@ -121,7 +121,7 @@ exports.isEmailConfigured = async (groupId = null) => { * @return {Promise} returns details about the attempt to send email, e.g. if it is successful; based on * nodemailer response. */ -exports.sendEmail = async (email, purpose, { groupId, user }) => { +exports.sendEmail = async (email, purpose, { groupId, user } = {}) => { const db = new CouchDB(GLOBAL_DB) const config = await getSmtpConfiguration(db, groupId) if (!config) {