diff --git a/desci-server/src/services/auth.ts b/desci-server/src/services/auth.ts index 193df0ea7..4ac1dc2c9 100644 --- a/desci-server/src/services/auth.ts +++ b/desci-server/src/services/auth.ts @@ -29,6 +29,7 @@ const registerUser = async (email: string) => { const magicLinkRedeem = async (email: string, token: string): Promise => { email = email.toLowerCase(); + if (!email) throw Error('Email is required'); logger.trace({ fn: 'magicLinkRedeem', email: hideEmail(email) }, 'auth::magicLinkRedeem'); const link = await client.magicLink.findFirst({ @@ -36,13 +37,26 @@ const magicLinkRedeem = async (email: string, token: string): Promise => { email, }, orderBy: { - createdAt: 'desc', + id: 'desc', }, }); if (!link) { throw Error('No magic link found for the provided email.'); } + logger.trace( + { + fn: 'magicLinkRedeem', + email: hideEmail(email), + tokenProvided: 'XXXX' + token.slice(-2), + tokenProvidedLength: token.length, + latestLinkFound: 'XXXX' + link.token.slice(-2), + linkEqualsToken: link.token === token, + latestLinkExpiry: link.expiresAt, + latestLinkId: link.id, + }, + '[MAGIC]auth::magicLinkRedeem comparison debug', + ); if (link.failedAttempts >= 5) { // Invalidate the token immediately @@ -176,10 +190,18 @@ const sendMagicLinkEmail = async (email: string, ip?: string) => { // .sendEmail(params) // .promise(); // const data = await sendPromise; - logger.info({ fn: 'sendMagicLinkEmail', email, msg }, 'Email sent'); + logger.info({ fn: 'sendMagicLinkEmail', email, tokenSent: 'XXXX' + token.slice(-2) }, '[MAGIC]Email sent'); } catch (err) { logger.error({ fn: 'sendMagicLinkEmail', err, email }, 'Mail error'); } + if (process.env.NODE_ENV === 'dev') { + // Print this anyway whilst developing, even if emails are being sent + const Reset = '\x1b[0m'; + const BgGreen = '\x1b[42m'; + const BgYellow = '\x1b[43m'; + const BIG_SIGNAL = `\n\n${BgYellow}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$${Reset}\n\n`; + logger.info(`${BIG_SIGNAL}Email sent to ${email}\n\nToken: ${BgGreen}${token}${Reset}${BIG_SIGNAL}`); + } return true; } else { const Reset = '\x1b[0m';