61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { APIEvent, json } from 'solid-start/api';
|
|
import { findUserByToken } from '~/lib/find-user-by-token';
|
|
import { readConfig } from '~/server-only-lib/read-config';
|
|
import { createTransport } from 'nodemailer';
|
|
import { resolve } from 'path';
|
|
import hbs from 'nodemailer-express-handlebars';
|
|
import { put } from '~/lib/put';
|
|
import { obfuscateMail } from '~/lib/obfuscate-mail';
|
|
|
|
export async function GET({ params }: APIEvent) {
|
|
const { credentials, mailer } = readConfig();
|
|
console.log({ caller: 'api/conf GET', params, credentials });
|
|
const user = await findUserByToken(params.token, credentials);
|
|
console.log({ caller: 'api/conf GET', params, credentials, user });
|
|
if (!user) {
|
|
return json({ params, response: 'NOT FOUND' });
|
|
}
|
|
const transporter = createTransport(mailer);
|
|
|
|
const handlebarOptions = {
|
|
viewEngine: {
|
|
partialsDir: resolve('src/mail-template/'),
|
|
defaultLayout: false,
|
|
},
|
|
viewPath: resolve('src/mail-template/'),
|
|
};
|
|
transporter.use('compile', hbs(handlebarOptions));
|
|
|
|
const code = [...Array(6)].map((_) => (Math.random() * 10) | 0).join('');
|
|
|
|
const mailOptions = {
|
|
from: '"Dyomedea app" <app@dyomedea.com>', // sender address
|
|
to: user.mail, // list of receivers
|
|
subject: 'Validation!',
|
|
template: 'email', // the name of the template file i.e email.handlebars
|
|
context: {
|
|
id: user._id,
|
|
code,
|
|
},
|
|
};
|
|
|
|
await transporter.sendMail(mailOptions);
|
|
|
|
await put(
|
|
user._id,
|
|
{
|
|
...user,
|
|
$: {
|
|
code,
|
|
expiration: new Date(Date.now() + 15 * 60 * 1000).toISOString(),
|
|
},
|
|
},
|
|
false,
|
|
undefined,
|
|
true,
|
|
credentials
|
|
);
|
|
|
|
return json({ params, status: 'OK', mail: obfuscateMail(user.mail) });
|
|
}
|