How to access cookies with ssr?

I have an app where you log in, which gives you two cookies, sessionId and userId. Then, on page load, I want to use that to show a little log in badge like the attached image, that calls a server function that returns user data, to then be put in the badge. How do I access cookies with ssr?
5 Replies
ChrisThornham
ChrisThornham•2w ago
You access cookies through Vinxi. Here's an example:
import { getCookie } from "vinxi/http";

// Then get the cookie by name.
const cookie = getCookie("cookie-name");
import { getCookie } from "vinxi/http";

// Then get the cookie by name.
const cookie = getCookie("cookie-name");
Hope that helps, Chris
ChrisThornham
ChrisThornham•2w ago
If you're interested in learning more about Vinxi, this link will help: https://vinxi.vercel.app/api/server/cookies.html
Cookies | Vinxi
Vinxi Documentation
a𒈙𒈙𒈙
But where do I put this I have been accessing the document object for cookies But it gives an error when I do
ChrisThornham
ChrisThornham•2w ago
I'll write a more detailed description when I get to my computer. Ok. You'll import getCookie at the top of your file. Next, you'll want to access your cookie on the server. You can do that in SolidStart with "use server";. Here's an example of how you can use "use server"; inside of a function.
import { getCookie } from "vinxi/http";

export const myFunction() {
"use server"; // this function will now run on the server.

// Get the cookie by name
const cookie = getCookie("cookie-name");

// Use the cookie
return cookie:
}
import { getCookie } from "vinxi/http";

export const myFunction() {
"use server"; // this function will now run on the server.

// Get the cookie by name
const cookie = getCookie("cookie-name");

// Use the cookie
return cookie:
}
a𒈙𒈙𒈙
Alright, thanks
Want results from more Discord servers?
Add your server