Skip to content
Back

[SOLVED!] Next js Appwrite cookie

  • 0
  • Tools
  • Accounts
  • Web
  • Cloud
shubhamm6996
25 Nov, 2023, 11:41

when i'm logging in and creating email session from client web sdk, it logs in user and creates and assigns cookie in cookie tab in developer tools, and also a fallback cookie in localstorage,

but when i refresh the cookie from cookie tab vanishes, may i know why is that? also the localStorage fallback cookie is still there (because session is not deleted yet, i.e, logout)

i'm trying to use middleware.js in next js to intercept the reqeust, if user manually changes the url, i need to redirect user, but since cookie vanishes from cookies tab i dont know how to achieve this?

please help

TL;DR
TL;DR: The user is trying to set cookies with Appwrite and Next.js but is facing issues. They are attempting to set cookies manually and are unsure why they are not working. They are also seeking guidance on how to handle redirection using middleware in Next.js. They mention the possibility of needing a custom domain for the cookies to work properly. Solution: Use the Appwrite SDK to set the cookies instead of setting them manually. Consider using a custom domain for better cookie stability. Check the documentation for custom domains on Appwrite's website.
Drake
25 Nov, 2023, 16:14

What are you using for your endpoint And is your app running on localhost?

shubhamm6996
25 Nov, 2023, 17:53

Endpoint: Appwrite cloud And yes localhost currently, later have to upload to ubuntu machine in aws

Drake
25 Nov, 2023, 18:05

So cookies are tied to the domain, not the app. So the cookies will be visible if you browse to the domain.

Because the domains are different, you'll probably also run into a problem where the Appwrite cookie isn't sent to Appwrite because it's considered a 3rd party cookie. To solve that, you'll need a custom domain: https://appwrite.io/docs/advanced/platform/custom-domains

shubhamm6996
25 Nov, 2023, 18:13

Okay got it, need a custom domain, with an appwrite subdomain to stabalise the cookie, Since it's not working on localhost I need to develop and deploy, then check if cookie is working

Drake
25 Nov, 2023, 18:17

To test locally, maybe you can edit your hosts file to fake using a domain

Drake
25 Nov, 2023, 18:18

Another thing about nextjs is if you're using server side rendering, you may run into problems because the session is client side and not server side

shubhamm6996
25 Nov, 2023, 18:20

Wow is it possible to do that, have to check that..

Yes thankyou so much for your help

discorrrdbomba
26 Nov, 2023, 17:41

hi Steven, I've got similar issues (but not exactly the same) trying to get appwrite auth working with nextjs (self hosted appwrite instance on a live domain (docker install on a ubuntu server, which itself runs smoothly and the dashboard is available live and works like a charme)): I managed to set the cookies using a nextjs route service, (at least I think they are set, since I can not have a look at them due to the discussed domain restriction)...but then when I try to trigger verification using another route file, it does not work out...any ideas...? object which i'm setting using nextjs' response.cookie.set() function: { a_session_653bd1dc5c2abcd7d741: 'eyJpZCI6IjZm....', expires: 'Mon, 25-Nov-2024 17:36:39 GMT', path: '/', domain: '.api.example.com', secure: 'true', httponly: 'true', samesite: 'None' } for example...is the dot in front of the "api.example.com" correct....?

discorrrdbomba
26 Nov, 2023, 17:48

maybe i should note, that i am trying to work from localhost, but the nextjs server/route itself does the POST request to the api domain, and receives the above mentioned object, then i am trying to set the cookie, returning the response with the appropriate data set as header/cookies using nextjs response.cookies.set(obj) function. That works and sets cookies correctly, as long as i filter out {domain: ".api.domain.com"} from the object. But i guess this is working, and i just can not see/read it, since i am not asking from the api-domain. If thats true, how does my verification not work out? IT shoud be a PUT request, as far as i know....and its code is similarly set up as in the above mentioned .../account/sessions/email request, but only for .../account/verification

ideclon
26 Nov, 2023, 17:57

You shouldn’t be setting the cookies yourself - the SDK should be setting them

discorrrdbomba
26 Nov, 2023, 18:24

also using REST / server side sessions? I could not imagine what for they'd publish some docs like this: https://appwrite.io/docs/references/cloud/server-rest/account (see platform dropdown on the upper right side (desktop version)) ... the thing is i am using nextjs server components / app-router, which decouples lot of stuff, from the original way to do it, using only plain react

discorrrdbomba
26 Nov, 2023, 18:25

but maybe you are right, do you know it for sure, not to use set them manually, also using REST/nextjs?

Drake
26 Nov, 2023, 18:31

Please create a separate post instead of posting in another person's thread

discorrrdbomba
26 Nov, 2023, 18:33

does this really make sense, in this case (regarding the title/header/subject name of this post/thread) - for sure if you think it does, i'll do so, but I thougt its not the person which is in focus, but the topic...

fafa
26 Nov, 2023, 19:48

Hey!

If this is solved for you, make sure to edit your thread title in the beginning with [Solved], Thanks! 😄

shubhamm6996
26 Nov, 2023, 21:16

[SOLVED!] Next js Appwrite cookie

shubhamm6996
26 Nov, 2023, 21:22

i shifted to flask for manual authentication and currently using only database from appwrite, with flask i can handle cookies and session with next js manually, but with appwrite, i need a separate domain to that or edit host files or even after that it may not work, or something will break with nextjs, etc. which is out of my way to do things i think.. but someone who has time to make it work and get to the bottom of this can try it out, ( since we can make it better by using it more ) since im on a time crunch, i thought this was the best solution for me currently, thankyou so much

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