From 774a2418ce4a4db34b0d7500971309a23d6bc0bd Mon Sep 17 00:00:00 2001 From: kadami <86646883+kadamidev@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:03:41 +0000 Subject: [PATCH] improve logs around auth --- desci-server/src/controllers/auth/magic.ts | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/desci-server/src/controllers/auth/magic.ts b/desci-server/src/controllers/auth/magic.ts index 94be7f10c..9282555ce 100644 --- a/desci-server/src/controllers/auth/magic.ts +++ b/desci-server/src/controllers/auth/magic.ts @@ -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'; @@ -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 @@ -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 @@ -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' }); } } };