Cookie not set in browser
Unanswered
Southern fire ant posted this in #help-forum
Southern fire antOP
Hello,
I am trying to create a login form for my api and get back the cookie. I am receiving cookies but they are not set in my browser.
Maybe the issue is that I am getting cookies on 'server-side' and not client-side?
Here is the code:
I am trying to create a login form for my api and get back the cookie. I am receiving cookies but they are not set in my browser.
Maybe the issue is that I am getting cookies on 'server-side' and not client-side?
Here is the code:
import { authenticate } from "../lib/actions";
export default function Login() {
return (
<>
<h1>Login</h1>
<form action={authenticate}>
<input type="text" name="uid" placeholder="uid" required />
<input
type="password"
name="password"
placeholder="Password"
required
/>
<button type="submit">Login</button>
</form>
</>
);
}"use server";
export async function authenticate(formData: FormData) {
try {
const response = await fetch("http://localhost:3333/auth/login", {
method: "POST",
body: JSON.stringify({
uid: formData.get("uid"),
password: formData.get("password"),
}),
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});
console.log(await response.json());
} catch (error) {
throw error;
}
}9 Replies
English Lop
Having the same problem
Maybe the issue is that I am getting cookies on 'server-side' and not client-side? That's what I thought of tooSouthern fire antOP
I want to avoid doing all my api calls on the client side
but if there is no other solution, ..
English Lop
did you find a solution @Southern fire ant ?
@English Lop `Maybe the issue is that I am getting cookies on 'server-side' and not client-side?` That's what I thought of too
that's very correct. @Southern fire ant you have to manually retrieve the cookie/token value in the action and set it again with cookies().set()
@joulev that's very correct. <@528986801910579216> you have to manually retrieve the cookie/token value in the action and set it again with cookies().set()
English Lop
Headers {
'x-powered-by': 'Express',
'access-control-allow-origin': 'http://localhost:3000',
vary: 'Origin',
'access-control-allow-credentials': 'true',
'set-cookie': 'sessionId=3acbd....
....
}this is what I get if I log the response in the server component, so basically I should get the
set-cookie value and store it with cookies().set()?@English Lop Headers {
'x-powered-by': 'Express',
'access-control-allow-origin': 'http://localhost:3000',
vary: 'Origin',
'access-control-allow-credentials': 'true',
'set-cookie': 'sessionId=3acbd....
....
}
this is what I get if I log the response in the server component, so basically I should get the `set-cookie` value and store it with cookies().set()?
yes. that's why typically for endpoints that are not to be called from the browser like this, it's better to send the token(s) directly in the (json) response than to use cookies.
English Lop
thank you a lot