Hey everyone :slightly_smiling_face: I have a ques...
# ory-network
b
Hey everyone 🙂 I have a question: Is it possible to use JWTs instead of cookies with Ory Network/Ory Cloud? I'm trying to deploy my application to production but I don't have a custom domain, meaning my client application will encounter CORS issues when trying to access Ory's Endpoints. Is it possible to use JWTs instead? I found this guide and I managed to generate a JWT and get it back, but I'm not sure what to do next (how to protect my API for example)
m
Hello @brash-raincoat-15175 I would recommend to get a custom domain if you are using Ory Network in production - if there are concerns about budget, feel free to reach out to me, I am sure we can find a solution. Are you using Ory for API authentication? I think that is the best use case for JWT and for most others using cookies is easier. As for the CORS issues if you send the cookie and CSRF token to your backend middleware it should work (even without custom domain I think.)
b
Hi @magnificent-energy-493, thanks for the quick reply. It's actually a matter of budget really. We saw that it costs 70$/month. We're a very early stage startup, and we've migrated from Auth0 to Ory because we wanted to allow our users to self-host our product.
About what you've mentioned (CORS issues) - if I'm not mistaken, our client sends a request to Ory to fetch the session, leading to a Set-Cookie in the response. If the client & Ory are not on the same domain, that won't work, right? To give a concrete example, our client sits at app.merlinn.co (hosted by Vercel). In our client code we do this:
Copy code
ory
      .toSession()
      .then(({ data }) => {
        // User has a session!
        setSession(data);
        ory.createBrowserLogoutFlow().then(({ data }) => {
          // Get also the logout url
          setLogoutUrl(data.logout_url);
        });
      })
      .catch((err) => {
        console.log("No session found", err);
      })
      .finally(() => {
        setLoading(false);
      });
This sets the cookie of Ory, which allows the browser to send the cookie to our server (because they're on the same domain. Our server sits on api.merlinn.co) In our server, we extract this cookie in an Express.js middleware and do this:
Copy code
export const checkAuth = async function (
  req: Request,
  res: Response,
  next: NextFunction,
) {
  try {
    const { data: session } = await ory.toSession({
      cookie: req.header("cookie"),
    });

    req.session = session;
    next();
  } catch (error) {
    next(error);
  }
};
Do you have an idea in mind of making it work without custom domain maybe? 🧐
To give more context, this is our project. It's an open source one. We're currently working on building our Cloud edition, which led us to investigate this subject.
m
I think you just need to include the CSRF token on the middleware. Here is how I do it in my frontend test app for example:
Copy code
try {
          const response = await fetch(`${apiBasePath}/user-data`, {
            headers: {
              "Content-Type": "application/json",
            },
            method: "GET",
            credentials: "include", // Include cookies + CSRF token in the request
          });
and then in my backend (just like you did) - i guess the difference is here I send all the cookies which also includes the CSRF token
Copy code
const { data: session } = await ory.toSession({ cookie: cookies });
I will DM you on options for the Production plan for early stage startups
b
@magnificent-energy-493 Thanks. However, I still don't understand whether the
ory.toSession()
on the client side should work. We do use
withCredentials
on our Ory client instance. However, without Ory tunnel locally (running on port 4000), the authentication doesn't work and we get CORS issues. If we deploy the app to production, the tunnel won't exist there. Wouldn't we have to use custom domain?
m
Your code there looks fine to me 🤔
If we deploy the app to production, the tunnel won't exist there. Wouldn't we have to use custom domain
Yes that is right. Since your app is under a different domain it will cause CORS issues. There might be workaround using e.g. Cloudflare Tunnel or similar solutions in production. But we haven't tested those so I cant guarantee it 😬
b
I see. So I guess we have to use custom domain in order to make our Ory Network the same domain as the application, which will allow the cookies to work. Is that correct?
1
👍 1