1
0
Fork 0
mirror of synced 2024-06-14 08:24:48 +12:00

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 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.")
}
}

View file

@ -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)
}

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