Skip to content

Commit e3360f8

Browse files
committed
chore: logging updates to magic links
1 parent be0c7db commit e3360f8

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/controllers/magicLinks.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import { User } from '../models/users';
1616
import { AuthEventService } from '../services/authEventService';
1717
import { sendMagicLinkEmail } from '../services/messagingService';
1818
import { AuthenticatedRequest } from '../types/types';
19+
import getLogger from '../utils/logger';
1920
import {
2021
computeSessionTimes,
2122
hashDeviceFingerprint,
2223
hashSha256,
2324
parseDurationToSeconds,
2425
} from '../utils/utils';
2526

27+
const logger = getLogger('magic-links');
28+
2629
const TTL_MINUTES = 15;
2730
const AUTH_MODE: 'web' | 'server' = process.env.AUTH_MODE! as 'web' | 'server';
2831

@@ -79,6 +82,7 @@ export async function requestMagicLink(req: Request, res: Response) {
7982
}
8083

8184
export async function verifyMagicLink(req: Request, res: Response) {
85+
logger.debug('Verifying magic link');
8286
const { token } = req.params;
8387

8488
if (!token) {
@@ -91,18 +95,23 @@ export async function verifyMagicLink(req: Request, res: Response) {
9195
});
9296

9397
if (!record) {
98+
logger.warn(`No magic link found for token: ${token}`);
9499
return res.status(400).json({ message: 'Invalid verification token' });
95100
}
96101

97102
if (record.used_at) {
103+
logger.warn(`Magic link token is already used ${token}`);
98104
return res.status(400).json({ message: 'Invalid verification token' });
99105
}
100106

101107
if (record.expires_at < new Date()) {
108+
logger.warn(`Magic link token expired: ${token}`);
102109
return res.status(400).json({ message: 'Invalid verification token' });
103110
}
104111

105112
// Atomic consume
113+
logger.info(`Magic link being consumed ${token}`);
114+
106115
const [updated] = await MagicLinkToken.update(
107116
{ used_at: new Date() },
108117
{
@@ -114,13 +123,15 @@ export async function verifyMagicLink(req: Request, res: Response) {
114123
);
115124

116125
if (!updated) {
126+
logger.error(`Magic link token was not consumted: ${token}`);
117127
return res.status(500).json({ message: 'Failed to use token' });
118128
}
119129

120130
await AuthEventService.log({
121131
userId: record.user_id,
122132
type: 'magic_link_success',
123133
req,
134+
metadata: { message: `Token: ${token}` },
124135
});
125136

126137
// Device binding check

src/lib/cookie.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export async function setAuthCookies(
5252
}
5353

5454
export function clearAuthCookies(res: Response) {
55-
logger.debug('Cookies cleared');
5655
res.clearCookie('seamless_access', {
5756
httpOnly: true,
5857
secure: process.env.NODE_ENV === 'production',

src/middleware/verifyCookieAuth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function verifyCookieAuth(cookieType: CookieType = 'access') {
3535
clearAuthCookies(res);
3636
return res.status(401).json({ error: 'unauthorized' });
3737
}
38-
logger.debug(`Validating ephemeral cookie`);
3938
const user = await validateSession({
4039
type: 'cookie',
4140
value: ephemeralCookie,
@@ -77,7 +76,7 @@ export function verifyCookieAuth(cookieType: CookieType = 'access') {
7776
}
7877

7978
// If we reach here, both access & refresh failed
80-
clearAuthCookies(res);
79+
//clearAuthCookies(res);
8180
return res.status(401).json({ error: 'unauthorized' });
8281
} catch (err) {
8382
logger.error('verifyCookieAuth error:', err);

0 commit comments

Comments
 (0)