import { Request, Response } from "express";
import Stacker from "../../middlewares/stacker.js";
import { GetUserMiddleware } from "../../middlewares/user.js";

import { getAccessTokenJWT } from "../../../helper/jwt.js";
import { getClientWithOrigin } from "./_helper.js";

export const GetJWTByUser = Stacker(
   GetUserMiddleware(true, false),
   async (req: Request, res: Response) => {
      const { client_id, origin } = req.query as { [key: string]: string };

      const client = await getClientWithOrigin(client_id, origin);

      const jwt = await getAccessTokenJWT({
         user: req.user,
         client: client,
         permissions: [],
      });

      res.json({ jwt });
   }
);
