73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import apiSendEmailCode from "../../functions/api/users/api-send-email-code";
|
|
import {
|
|
APIResponseObject,
|
|
APISendEmailCodeFunctionParams,
|
|
SendEmailCodeParams,
|
|
} from "../../types";
|
|
import grabUserDSQLAPIPath from "../../utils/backend/users/grab-api-path";
|
|
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
|
|
|
/**
|
|
* # Send Email Code to a User
|
|
*/
|
|
export default async function sendEmailCode(
|
|
params: SendEmailCodeParams
|
|
): Promise<APIResponseObject> {
|
|
const {
|
|
apiKey,
|
|
email,
|
|
database,
|
|
temp_code_field_name,
|
|
mail_domain,
|
|
mail_password,
|
|
mail_username,
|
|
mail_port,
|
|
sender,
|
|
response,
|
|
extraCookies,
|
|
useLocal,
|
|
apiVersion,
|
|
dbUserId,
|
|
} = params;
|
|
|
|
const defaultTempLoginFieldName = "temp_login_code";
|
|
const emailLoginTempCodeFieldName = temp_code_field_name
|
|
? temp_code_field_name
|
|
: defaultTempLoginFieldName;
|
|
|
|
const emailHtml = `<p>Please use this code to login</p>\n<h2>{{code}}</h2>\n<p>Please note that this code expires after 15 minutes</p>`;
|
|
|
|
const apiSendEmailCodeParams: APISendEmailCodeFunctionParams = {
|
|
database,
|
|
email,
|
|
email_login_field: emailLoginTempCodeFieldName,
|
|
html: emailHtml,
|
|
mail_domain,
|
|
mail_password,
|
|
mail_port,
|
|
mail_username,
|
|
sender,
|
|
response,
|
|
extraCookies,
|
|
dbUserId,
|
|
};
|
|
|
|
if (useLocal) {
|
|
return await apiSendEmailCode(apiSendEmailCodeParams);
|
|
} else {
|
|
const httpResponse: APIResponseObject = await queryDSQLAPI({
|
|
path: grabUserDSQLAPIPath({
|
|
paradigm: "auth",
|
|
action: "send-email-code",
|
|
database,
|
|
apiVersion,
|
|
}),
|
|
apiKey,
|
|
body: apiSendEmailCodeParams,
|
|
method: "POST",
|
|
});
|
|
|
|
return httpResponse;
|
|
}
|
|
}
|