Skip to content

Commit

Permalink
Merge pull request #734 from desci-labs/signin-dbg
Browse files Browse the repository at this point in the history
More logs
  • Loading branch information
kadamidev authored Dec 23, 2024
2 parents 0aafd70 + eda4c91 commit c65100f
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions desci-server/src/controllers/auth/magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ export const magic = async (req: Request, res: Response, next: NextFunction) =>

if (process.env.NODE_ENV === 'production') {
if (code) {
logger.info(
{ fn: 'magic', email: req.body.email },
`[MAGIC] User attempting to auth with magic code: XXXX${code.slice(-2)}`,
);
logger.info({ 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}`,
);
logger.info({ email: req.body.email }, `[MAGIC] User requested a magic code, cleanEmail: ${cleanEmail}`);
}
} else {
logger.info({ fn: 'magic', reqBody: req.body }, `magic link`);
Expand Down Expand Up @@ -86,18 +80,21 @@ export const magic = async (req: Request, res: Response, next: NextFunction) =>
try {
const ip = req.ip;
const ok = await sendMagicLink(cleanEmail, ip);
res.send({ ok });
logger.info({ ok }, 'Magic link sent');
res.send({ ok: !!ok });
} catch (err) {
logger.error({ ...err, fn: 'magic' });
logger.error({ err }, 'Failed sending code');
res.status(400).send({ ok: false, error: 'Failed sending code' });
}
} else {
// we are validating the magic code is correct
try {
const user = await magicLinkRedeem(cleanEmail, code);

if (!user) throw new Error('User not found');

if (orcid && user) {
logger.trace({ fn: 'magic', orcid }, `setting orcid for user`);
logger.trace({ orcid }, `setting orcid for user`);

if (!user.name) {
const orcidRecord = await getOrcidRecord(orcid, access_token);
Expand All @@ -124,12 +121,14 @@ export const magic = async (req: Request, res: Response, next: NextFunction) =>
// TODO: Bearer token still returned for backwards compatability, should look to remove in the future.
res.send({ ok: true, user: { email: user.email, token, termsAccepted } });

logger.info('[MAGIC] User logged in successfully');

if (!termsAccepted) {
// saveInteraction(req, ActionType.USER_TERMS_CONSENT, { userId: user.id, email: user.email }, user.id);
}
saveInteraction(req, ActionType.USER_LOGIN, { userId: user.id }, user.id);
} catch (err) {
logger.error({ ...err, fn: 'magic' });
logger.error({ err }, 'Failed redeeming code');
res.status(400).send({ ok: false, error: 'Failed redeeming code' });
}
}
Expand Down

0 comments on commit c65100f

Please sign in to comment.