Cookies not getting sent to remote server
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Hi, I'm running my dev server locally for client-side and I'm using next auth for session management. I have a remote external server which is the actual backend for my app. I'm getting a token from the API that I want to set inside a cookie from my [...nextauth]/route.ts for my external server (meaning every request to remove an external server should be able to access the cookie). I'm not able to view the cookie inside the browser and it is not even getting sent to the backend. From the backend, I've enabled credentials and allowedOrigins.
CredentialsProvider({
id: "credentials",
name: "Email and Password",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string;
password: string;
};
const apiUrl = `${BASE_URL}${url.login}`;
const res = await fetch(apiUrl, {
method: "POST",
body: JSON.stringify({
email,
password,
}),
headers: { "Content-Type": "application/json" },
});
const user = await res.json();
if (res.ok && user) {
const userResponse = user.response as User & {
accessToken: string;
};
const token = userResponse.accessToken;
// set cookie
cookies().set("accessToken", token, {
domain: "myapp.ap-south-1.awsapprunner.com", // this domain
path: "/",
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 60 * 60 * 10000),
});
return userResponse;
}
return null;
},
}),
1 Reply
Miniature Pinscher
Your server's domain has to be the same domain as your frontend. Meaning you will need to point a subdomain of your frontend domain name to your backend server.