Cookie是'在Svelte Kit中节省路线之间的费用



我对SvelteKit有点陌生,我正在尝试将cookie(特别是JWT令牌(从路由保存到服务器端的另一个路由(使用+page.server.ts+layout.server.ts(

这是我目前编码的:

// /account/sign-in/+page.server.ts
import { invalid, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
// ... other imports ...
export const actions: Actions = {
default: async ({ request, cookies }) => {
// conditionals to check
console.log("cookie before creation", cookies.get("auth")); // undefined
cookies.set("auth", "abc", { path: "/", maxAge: 60 * 60 * 24 * 365, httpOnly: true }); // Creates the cookie
console.log("cookie after creation", cookies.get("auth")); // "abc"
throw redirect(302, '/client/app'); // Redirect to /client/app
})
}
// /client/+layout.server.ts
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = async ({ cookies }) => {
const jwt = cookies.get('auth');
console.log("layout token", jwt); // undefined -- I expected: "abc"
}

上面的代码是在注释掉我的一些其他代码,特别是条件语句后工作的,例如,我有prisma代码来检查数据库,并将加密的密码与输入的密码进行比较,我注释掉了该代码,并将带有伪输入的cookie代码移到外部,以便它运行。在检查了每个条件语句以查看哪些有效/无效后,我的代码按预期运行。

最新更新