import { Request, Response } from "express";
import Stacker from "../../middlewares/stacker";
import { GetUserMiddleware } from "../../middlewares/user";
import { URL } from "url";
import Client from "../../../models/client";
import RequestError, { HttpStatusCode } from "../../../helper/request_error";
import { getAccessTokenJWT } from "../../../helper/jwt";
import { getClientWithOrigin } from "./_helper";

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 });
   }
);
