Finishing invite send email.

This commit is contained in:
mike12345567 2021-05-05 15:17:15 +01:00
parent 7bc3514fc1
commit b4beb4d8da
3 changed files with 34 additions and 23 deletions

View File

@ -47,7 +47,7 @@ exports.reset = async ctx => {
const { email } = ctx.request.body const { email } = ctx.request.body
const configured = await isEmailConfigured() const configured = await isEmailConfigured()
if (!configured) { 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 { try {
const user = await getGlobalUserByEmail(email) const user = await getGlobalUserByEmail(email)
@ -65,16 +65,17 @@ exports.reset = async ctx => {
*/ */
exports.resetUpdate = async ctx => { exports.resetUpdate = async ctx => {
const { resetCode, password } = ctx.request.body const { resetCode, password } = ctx.request.body
const userId = await checkResetPasswordCode(resetCode) try {
if (!userId) { const userId = await checkResetPasswordCode(resetCode)
throw "Cannot reset password." const db = new CouchDB(GLOBAL_DB)
} const user = await db.get(userId)
const db = new CouchDB(GLOBAL_DB) user.password = await hash(password)
const user = await db.get(userId) await db.put(user)
user.password = await hash(password) ctx.body = {
await db.put(user) message: "password reset successfully.",
ctx.body = { }
message: "password reset successfully.", } catch (err) {
ctx.throw(400, "Cannot reset password.")
} }
} }

View File

@ -5,8 +5,9 @@ const {
StaticDatabases, StaticDatabases,
} = require("@budibase/auth").db } = require("@budibase/auth").db
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
const { UserStatus } = require("../../../constants") const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
const { checkResetPasswordCode, checkInviteCode } = require("../../../utilities/redis") const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email")
const FIRST_USER_EMAIL = "test@test.com" const FIRST_USER_EMAIL = "test@test.com"
const FIRST_USER_PASSWORD = "test" const FIRST_USER_PASSWORD = "test"
@ -124,18 +125,27 @@ exports.find = async ctx => {
} }
exports.invite = 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 => { exports.inviteAccept = async ctx => {
const { inviteCode } = ctx.request.body const { inviteCode } = ctx.request.body
const email = await checkInviteCode(inviteCode) try {
if (!email) { const email = await checkInviteCode(inviteCode)
throw "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)
} 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)
} }

View File

@ -121,7 +121,7 @@ exports.isEmailConfigured = async (groupId = null) => {
* @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on * @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
* nodemailer response. * nodemailer response.
*/ */
exports.sendEmail = async (email, purpose, { groupId, user }) => { exports.sendEmail = async (email, purpose, { groupId, user } = {}) => {
const db = new CouchDB(GLOBAL_DB) const db = new CouchDB(GLOBAL_DB)
const config = await getSmtpConfiguration(db, groupId) const config = await getSmtpConfiguration(db, groupId)
if (!config) { if (!config) {