Skip to content
Back

Use JWT to allow Client Side interactions after SSR Auth?

  • 1
  • Auth
Flyto
10 Feb, 2025, 12:06

Hello everyone, I am rebuilding a CSR only application with NextJS to leverage SSR strategically so I can fetch certain data on the server and distribute it to all users equally. I figured if I have SSR its beneficial, security wise, to use SSR Authentication, right? That works well with Appwrite so far, cookie is stored and I can make requests. However, I cannot seem to figure out how to also allow the client to access appwrite directly via the clientSDK (to use server resources efficiently and improve performance I want to keep the majority of api calls on the client side).

TypeScript
cookieStorage.set(`a_session_${BACKEND_PROJECT}`, session.secret, {
  httpOnly: true,
  secure: true,
  sameSite: "none",
  maxAge: Math.floor(
    (new Date(session.expire).getTime() - Date.now()) / 1000
  ),
  path: "/",
  domain: BACKEND_DOMAIN,
});

I found this somewhere here in the support thread to store a cookie with the session secret in its name and the backend domain as domain (e.g. appwrite.io). However, as I am developing on localhost I cannot set that cookie. I have been searching for countless hours now trying to figure out a best practice to authenticate using SSR and using CSR for majority of data requests. Can someone please help me and shine some light? Neither Appwrite Docs nor NextJsDocs nor ChatGPT were any help. Appreciate it and thanks a lot 🙂

TL;DR
Rebuilding CSR app with NextJS for SSR but unsure about using SSR authentication or JWT for client-side interactions. Looking for best practice to authenticate with SSR and utilize CSR for data requests. Stored cookie with session secret and backend domain, facing issue on localhost. Any guidance on this setup?
Flyto
10 Feb, 2025, 12:20

Or would the better approach be to authenticate with CSR and then use a JWT where beneficial to handle requests via the server?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more