
I'm building a Next.js 14 application with tRPC and Appwrite for authentication as a way to further my learning, so I may have aspects of this wrong. This is a self-hosted 1.6 version of Appwrite. The T3 Stack was used as a starting point for this app.
I'm encountering an issue where the session cookie is not being set or persisted after a successful login.
Environment:
- Next.js 14 (App Router)
- tRPC
- Appwrite for authentication
- Running on localhost with custom subdomain (my.example.localhost:3000)
Steps to reproduce:
- User navigates to the sign-in page
- User enters credentials and submits the form
- The server successfully authenticates with Appwrite and receives a session token
- The server attempts to set a cookie with the session token
- The user is redirected to the account page
- The middleware checks for the presence of the session cookie
- The cookie is not found, and the user is redirected back to the sign-in page
Expected behaviour: After successful authentication, the session cookie should be set and persisted, allowing the user to access protected routes.
Actual behavior: The session cookie is not being set or persisted, resulting in the user being unable to access protected routes and getting stuck in a redirect loop.
Relevant code (see attached files):
- Auth Router (
src/server/api/routers/auth.ts
): - Middleware (
src/middleware.ts
): - Sign-in Page (
src/app/(auth)/signin/page.tsx
):
Server logs (see attached files):
Browser console logs (see attached files):
I've tried setting the cookie using both the cookies()
API from Next.js and manually setting the Set-Cookie
header. Neither approach seems to result in the cookie being set or persisted in the browser.
Any help or insights would be greatly appreciated. Thank you!

I appreciate a markdown file isn't the easiest way to access the code, so I have a gist with the same code here: https://gist.github.com/danmaby/f34c175459164941f7cbd3a43808b3ed.
Recommended threads
- User Authentication through REST API
Hey everyone, I’m looking for some guidance on the best way to authenticate frontend requests to my API gateway using Appwrite sessions. Right now, I'm trying ...
- Problem for certificate
Good evening! I installed Appwrite on my hosting via Docker Compose. Then I set up the .env configuration. But for some reason it does not support HTTPS, when l...
- Delete account on authentication
I'm building an authentication that need to a user a choice of delete their account when they need and on the documentation, if I find delete sessions or sessio...
