Skip to content

Commit

Permalink
improve logs around auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kadamidev committed Dec 23, 2024
1 parent f3b0811 commit 774a241
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions desci-server/src/controllers/auth/magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';

import { prisma as prismaClient } from '../../client.js';
import { logger } from '../../logger.js';
import { logger as parentLogger } from '../../logger.js';
import { magicLinkRedeem, sendMagicLink } from '../../services/auth.js';
import { contributorService } from '../../services/Contributors.js';
import { saveInteraction } from '../../services/interactionLog.js';
Expand All @@ -20,15 +20,33 @@ export const oneYear = 1000 * 60 * 60 * 24 * 365;
export const oneDay = 1000 * 60 * 60 * 24;
export const oneMinute = 1000 * 60;
export const magic = async (req: Request, res: Response, next: NextFunction) => {
const { email, code, dev, orcid, access_token, refresh_token, expires_in } = req.body;
const cleanEmail = email.toLowerCase().trim();

const logger = parentLogger.child({
module: '[Auth]::Magic',
email: email,
cleanEmail: cleanEmail,
code: `${code ? 'XXXX' + code.slice(-2) : ''}`,
orcid,
});

if (process.env.NODE_ENV === 'production') {
logger.info({ fn: 'magic', email: req.body.email }, `magic link requested`);
if (code) {
logger.info(
{ fn: 'magic', email: req.body.email },
`[MAGIC] User attempting to auth with magic code: XXXX${code.slice(-2)}`,
);
} else {
logger.info(
{ fn: 'magic', email: req.body.email },
`[MAGIC] User requested a magic code, cleanEmail: ${cleanEmail}`,
);
}
} else {
logger.info({ fn: 'magic', reqBody: req.body }, `magic link`);
}

const { email, code, dev, orcid, access_token, refresh_token, expires_in } = req.body;
const cleanEmail = email.toLowerCase().trim();

if (!code) {
// we are sending the magic code

Expand Down Expand Up @@ -71,7 +89,7 @@ export const magic = async (req: Request, res: Response, next: NextFunction) =>
res.send({ ok });
} catch (err) {
logger.error({ ...err, fn: 'magic' });
res.status(400).send({ ok: false, error: err.message });
res.status(400).send({ ok: false, error: 'Failed sending code' });
}
} else {
// we are validating the magic code is correct
Expand Down Expand Up @@ -111,7 +129,8 @@ export const magic = async (req: Request, res: Response, next: NextFunction) =>
}
saveInteraction(req, ActionType.USER_LOGIN, { userId: user.id }, user.id);
} catch (err) {
res.status(200).send({ ok: false, error: err.message });
logger.error({ ...err, fn: 'magic' });
res.status(400).send({ ok: false, error: 'Failed redeeming code' });
}
}
};

0 comments on commit 774a241

Please sign in to comment.