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,10 +65,8 @@ exports.reset = async ctx => {
*/ */
exports.resetUpdate = async ctx => { exports.resetUpdate = async ctx => {
const { resetCode, password } = ctx.request.body const { resetCode, password } = ctx.request.body
try {
const userId = await checkResetPasswordCode(resetCode) const userId = await checkResetPasswordCode(resetCode)
if (!userId) {
throw "Cannot reset password."
}
const db = new CouchDB(GLOBAL_DB) const db = new CouchDB(GLOBAL_DB)
const user = await db.get(userId) const user = await db.get(userId)
user.password = await hash(password) user.password = await hash(password)
@ -76,6 +74,9 @@ exports.resetUpdate = async ctx => {
ctx.body = { ctx.body = {
message: "password reset successfully.", message: "password reset successfully.",
} }
} catch (err) {
ctx.throw(400, "Cannot reset password.")
}
} }
exports.logout = async ctx => { exports.logout = async ctx => {

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
try {
const email = await checkInviteCode(inviteCode) const email = await checkInviteCode(inviteCode)
if (!email) {
throw "Unable to create new user, invitation invalid."
}
// redirect the request // redirect the request
delete ctx.request.body.inviteCode delete ctx.request.body.inviteCode
ctx.request.body.email = email ctx.request.body.email = email
// this will flesh out the body response // this will flesh out the body response
await exports.save(ctx) await exports.save(ctx)
} catch (err) {
ctx.throw(400, "Unable to create new user, invitation invalid.")
}
} }

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) {