如何创建一个安全且可在Sveltekit的多个地方使用的函数。

huangapple go评论40阅读模式
英文:

How to create a function that is secure and can be used in many places in Sveltekit

问题

我阅读了关于 SvelteKit 中 +page.server.ts 文件的一部分手册,并且我们有允许加载/获取、发送、更新和删除方法的方法/函数,但我想要一个可以在多个地方使用的方法/函数。我已经在我的 utils.ts 文件中创建了一个,但我想知道是否暴露了我的 API 令牌,因为我必须将这个方法导入到我的 +page.svelte 文件中。

英文:

I have read part of the manual about +page.server.ts files for sveltekit and we have methods/functions that allow the load/get, post, patch and delete methods but I would like to have a method/function that can be used in many places. I have created one in my utils.ts file but I wonder if I'm exposing my API tokens since I have to import this method into my +page.svelte

答案1

得分: 1

你在 +page.svelte+page.js 中导入的任何内容都会被暴露给客户端,这包括在那里使用的任何 API 令牌。

你可能想要做的是使用 +server.js 路由创建自己的 API 端点,并使用常规的 fetch 或表单提交到端点,然后该端点再使用外部 API 和令牌。由于这是完全在服务器端进行的操作,不会将任何内容暴露给客户端。

英文:

Anything you import in +page.svelte or +page.js is exposed to the client, so that would include any API tokens used there.

What you probably want to do is making your own API endpoints using +server.js routes and use regular fetches or forms to that endpoint which in turn uses your external API and the tokens. Since that would be fully server side nothing is exposed to the client.

huangapple
  • 本文由 发表于 2023年6月22日 05:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定