admin管理员组文章数量:1403123
I am trying offical SvelteKit example /.
Its code is hosted at
login and everything works fine when I run npm run dev
but when I run npm run dev -- --host
then login does not work.
cookies.set('jwt', value, { path: '/' });
This is not working so cookies are not getting set so login is not working.
How can I make login working when using --host
option?
I am trying offical SvelteKit example https://realworld.svelte.dev/.
Its code is hosted at https://github./sveltejs/realworld
login and everything works fine when I run npm run dev
but when I run npm run dev -- --host
then login does not work.
cookies.set('jwt', value, { path: '/' });
This is not working so cookies are not getting set so login is not working.
How can I make login working when using --host
option?
1 Answer
Reset to default 10I'm assuming you're blocked from logging in when addressing the exposed network IP URL (localhost
should still work) on an unsecured (http
) connexion.
The reason for this is because the default cookie configuration in SvelteKit is to set the secure
option to true
, meaning cookies won't get set upon unsecured requests. From the SvelteKit docs:
The httpOnly and secure options are true by default (except on http://localhost, where secure is false), and must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. The sameSite option defaults to lax.
As you can see, this explains why the default configuration works on localhost
(where secure
is false
) but not on the exposed network IP address (where secure
will be true
).
If you update the cookie configuration in src/routes/login/+page.server.js
to explicitly set the secure
option:
cookies.set('jwt', value, { secure: false, path: '/' });
and restart your server, you should then be able to login as expected from the exposed network IP URL (I was able to).
Note: in a production deployment, you will want the secure
flag set to true
and use a secure protocol (https
) for your requests.
本文标签:
版权声明:本文标题:javascript - Cookies are not getting set when running with --host option in official SvelteKit example - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744380352a2603471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论