-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.artifacts.ts
More file actions
28 lines (22 loc) · 873 Bytes
/
Copy pathauth.artifacts.ts
File metadata and controls
28 lines (22 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { randomBytes } from 'node:crypto';
const SIX_DIGIT_UPPER_BOUND = 1_000_000;
const generateHexToken = () => randomBytes(32).toString('hex');
const generateSixDigitCode = () => {
const value = randomBytes(4).readUInt32BE(0) % SIX_DIGIT_UPPER_BOUND;
return value.toString().padStart(6, '0');
};
const generateExpiryTimestamp = (hours: number) => {
const expiry = new Date();
expiry.setHours(expiry.getHours() + hours);
return expiry.toISOString();
};
export const createVerificationArtifacts = () => ({
verificationToken: generateHexToken(),
verificationCode: generateSixDigitCode(),
verificationTokenExpiresAt: generateExpiryTimestamp(24)
});
export const createPasswordResetArtifacts = () => ({
passwordResetToken: generateHexToken(),
passwordResetCode: generateSixDigitCode(),
passwordResetTokenExpiresAt: generateExpiryTimestamp(1)
});