Reorganizing
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-05-03 11:12:59 -04:00
parent 27a78dd471
commit dc72cefece
23 changed files with 163 additions and 87 deletions

View File

@@ -1,13 +1,34 @@
import crypto from 'crypto';
import { sign } from './jwt';
import { LOGIN_VALID_TIME, RESET_VALID_MINUTES } from '../constants/constants';
import { Status } from '../constants/auth';
const parseLoginValid = () => {
const [number, unit] = process.env.LOGIN_VALID_TIME || LOGIN_VALID_TIME;
return [
unit === 'd' ? parseInt(number) : 1,
unit === 'h' ? parseInt(number) : (unit === 'm' && 1) || 24,
unit === 'm' ? parseInt(number) : 60,
];
};
export const generateLoginToken = (sub: string, status: Status) => {
const [days, hours, mins] = parseLoginValid();
return sign({
sub,
status,
exp: Date.now() + days * hours * mins * 60 * 1000,
});
};
export const generateResetToken = (sub: string) => {
const hoursValid = <number>(process.env.RESET_VALID_HOURS || RESET_VALID_MINUTES);
const key = crypto.randomBytes(16).toString('hex');
const token = sign({
sub,
key,
exp: Date.now() + 24 * 60 * 60 * 1000,
exp: Date.now() + hoursValid * 60 * 60 * 1000,
});
return { key, token };
};