Skip to content

Commit

Permalink
minor adjustments to link query, improve debugging auth further
Browse files Browse the repository at this point in the history
  • Loading branch information
kadamidev committed Dec 23, 2024
1 parent eda4c91 commit 0d43f86
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions desci-server/src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,34 @@ const registerUser = async (email: string) => {

const magicLinkRedeem = async (email: string, token: string): Promise<User> => {
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({
where: {
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
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 0d43f86

Please sign in to comment.