MicroHack-Registrations-RestAPI / src / utils / Jwt.ts
Jwt.ts
Raw
import jwt from 'jsonwebtoken';
import { CookieOptions, Response } from 'express';
import { JWT_SECRET } from '../config/CheckableEnv';

export const generateToken = (res: Response, payload: MyPayload) => {
  const token = jwt.sign(payload, JWT_SECRET);

  const options: CookieOptions = {
    sameSite: 'none',
    httpOnly: true,
    expires: new Date(new Date().getTime() + 720000000),
    secure : true,
  };

  res.cookie('token', token, options);
};

export const verifyToken = (token: string) => {
  return jwt.verify(token, JWT_SECRET) as MyPayload;
};