AbhishekA
Convex Community3y ago
10 replies
Abhishek

How to get url of the domain from the http action?

I am trying to redirect the user to frontend after I have done some validation on http action and I am using this util function to getURL

export const getURL = () => {
  let url =
    process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
    process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
    "http://localhost:3000/";
  // Make sure to include `https://` when not localhost.
  url = url.includes("http") ? url : `https://${url}`;
  // Make sure to include a trailing `/`.
  url = url.charAt(url.length - 1) === "/" ? url : `${url}/`;
  return url;
};


and In my http action I am doing this :
 if (errorQuery) {
    const errorQuery = params.get("error_description");
    console.log("Api Logs | Error in LinkedIn Api ", errorQuery);
    return Response.redirect(
      `${getURL()}/dashboard/settings?message=${errorQuery}`,
      302
    );
  }

But its throwing error :

{
"code": "Uncaught ReferenceError: window is not defined",
"trace": "Uncaught ReferenceError: window is not defined\n    at getURL (../../node_modules/next/src/shared/lib/utils.ts:333:17)\n    at <anonymous> (../../convex/oauth.ts:68:6)\n    at async invokeFunction (../../node_modules/convex/src/server/impl/registration_impl.ts:70:11)\n    at async invokeHttpAction (../../node_modules/convex/src/server/impl/registration_impl.ts:397:0)\n    at async HttpRouter.runRequest (../../node_modules/convex/src/server/router.ts:275:16)\n"
}
Was this page helpful?