This commit is contained in:
@@ -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 };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user