Back

Sveltekit SSR

  • 0
  • Accounts
  • Web
djcali
13 Aug, 2023, 04:22

Hello,

Using mostly all of the code, besides the UI from https://github.com/Meldiron/appwrite-ssr-svelte-kit/tree/main to get SSR working in a demo project i'm using to learn how it all works.

I have it working on the server side but not sure what i'm doing wrong, the client side is not getting the account. I'm trying to learn here so don't make me feel stupid. lol!

Cookies are being set and I can see them in the browser. I am thinking the following code is where i'm messing up. I am using localhost Svelte and my Appwrite server is hosted.

TypeScript
export const AppwriteHostname: string = 'appwrite.hinrgdjs.com';

export const AppwriteEndpoint = import.meta.env.VITE_APP_ENDPOINT.toString();
export const AppwriteProject = import.meta.env.VITE_APP_PROJECT.toString();```

When I click ``Create Anyomous Account`` as expected the account gets created in my project and the cookies are set.(see pic)

when trying to fetch the account or delete the session, i get the 401 error:

```Request URL:
https://appwrite.hinrgdjs.com/v1/account
Request Method:
GET
Status Code:
401
Remote Address:
159.203.112.158:443
Referrer Policy:
strict-origin-when-cross-origin```

```Access-Control-Allow-Credentials:
true

Access-Control-Allow-Headers:
Origin, Cookie, Set-Cookie, X-Requested-With, Content-Type, Access-Control-Allow-Origin, Access-Control-Request-Headers, Accept, X-Appwrite-Project, X-Appwrite-Key, X-Appwrite-Locale, X-Appwrite-Mode, X-Appwrite-JWT, X-Appwrite-Response-Format, X-SDK-Version, X-SDK-Name, X-SDK-Language, X-SDK-Platform, X-SDK-GraphQL, X-Appwrite-ID, X-Appwrite-Timestamp, Content-Range, Range, Cache-Control, Expires, Pragma
Access-Control-Allow-Methods:
GET, POST, PUT, PATCH, DELETE
Access-Control-Allow-Origin:
http://localhost:5174
TL;DR
The user is experiencing an issue with Sveltekit SSR. The server-side is working fine, but the client-side is not getting a cookie. They have provided their code and mentioned that the appwrite-ssr npm package can be used for this. They have also mentioned their .env file and hosts file entries. Other users have suggested checking response headers and logs, using a different browser, and confirming if the hosts file is correctly set up. It is recommended to change the SsrHostname in the code and edit the hosts file to point to localhost. The user is also advised to create a local appwrite server for testing. The
Drake
13 Aug, 2023, 04:28

The most important part is appwrite endpoint must be a subdomain of your app. This makes the cookie a 1st party cookie that will be shared and sent to both your back end and Appwrite

djcali
13 Aug, 2023, 04:56

Ok, so I won’t be able to use localhost for the app and use my appwrite hosted. I’ll have to create a local appwrite server for testing?

Drake
13 Aug, 2023, 04:57

I recommend editing your hosts file to point hinrgdjs.com to localhost

djcali
13 Aug, 2023, 05:12

So I should change export const SsrHostname: string = 'localhost'; to export const SsrHostname: string = 'hinrgdjs.com' and in my hosts file hinrgdjs.com localhost

djcali
13 Aug, 2023, 05:23

in my hosts file on my local machine I have an entry: 127.0.0.1 hinrgdjs.com and when i ping hinrgdjs.com, I am getting 127.0.0.1 back. Is this correct?

djcali
14 Aug, 2023, 20:03

So in the browser, this still goes to the hinrgdjs.com, only when i ping from the terminal does it report back 127.0.0.1. So after i build the svelte app in dev mode and it opens up in the browser as expected it doesnt work. I know the code works perfectly because I actually built the app and used it on the real hosted server and everything works, i am able to set the cookies and do a fetch request same as in the Meldiron example.

Unfortunately I cant get it to work in dev. Any help apprecitated. Thanks.

Drake
14 Aug, 2023, 20:28

I have heard some browsers ignore the hosts file

djcali
14 Aug, 2023, 21:07

ok, ill try another browser

djcali
14 Aug, 2023, 21:45

I used Chrome and that did honor the hosts file, because now the browser gives the "This site can not be reached " message. Still not working, I get the anon user in appwrite but no cookie created in the browser. I get the cookie when i change back to localhost. hmmm

Drake
14 Aug, 2023, 21:46

but no cookie created in the browser.

What makes you say that?

Drake
14 Aug, 2023, 21:47

are you connecting to a local appwrite?

djcali
14 Aug, 2023, 21:48

no

Drake
14 Aug, 2023, 21:49

anything in any logs?

Drake
14 Aug, 2023, 21:49

you should be able to see stuff in the response headers too

djcali
14 Aug, 2023, 21:49

Just so im clear, here is what im doing.

my .env file:

TypeScript
VITE_APP_PROJECT="624293b9aed2a2244ed3"
VITE_APP_SSR_HOSTNAME="linkup.hinrgdjs.com"
VITE_APP_APPWRITE_HOSTNAME="appwrite.linkup.hinrgdjs.com"```

My hosts file has the entry: ``127.0.0.1 linkup.hinrgdjs.com``
djcali
14 Aug, 2023, 21:51

Sorry i meant the client side is not getting a cookie

djcali
14 Aug, 2023, 21:51

the server side is working, it creates the anon user

otik
13 Oct, 2023, 18:38

Hi, you can use appwrite-ssr npm package for this all. You dont have to worry about that all.

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