brash-raincoat-15175
08/06/2024, 12:42 PMmagnificent-energy-493
brash-raincoat-15175
08/06/2024, 1:16 PMbrash-raincoat-15175
08/06/2024, 1:18 PMory
.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:
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);
}
};
brash-raincoat-15175
08/06/2024, 1:18 PMbrash-raincoat-15175
08/06/2024, 1:21 PMmagnificent-energy-493
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
const { data: session } = await ory.toSession({ cookie: cookies });
magnificent-energy-493
brash-raincoat-15175
08/06/2024, 2:06 PMory.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?magnificent-energy-493
If we deploy the app to production, the tunnel won't exist there. Wouldn't we have to use custom domainYes 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 😬
brash-raincoat-15175
08/06/2024, 2:15 PM