Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 store 中并未对 localStorage 的 TOKEN_NAME 键进行赋值的缺陷 #504

Merged
merged 4 commits into from
May 18, 2023

Conversation

SpringHgui
Copy link
Contributor

@SpringHgui SpringHgui commented May 12, 2023

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 Issue

💡 需求背景和解决方案

  1. 底层携带tokenlocalStorage中获取token,而store中并未对localStorageTOKEN_NAME键进行赋值,sotore做了持久化,亦可修改下面的获取方式从userStore中获取
    image

  2. 另外,这里默认开启withCredentials则在跨域请求时对后端的安全性要求更高,强制要求Access-Control-Allow-Origin必须返回当前域,axios组件该参数默认貌似是关闭的,是否应该当开发者明确自己的需求必须携带cookie时主动打开该配置,而不是默认开启,因为大部分项目应该不需要携带cookie。
    image

以上属于个人简单测试后的拙见,仅供参考

📝 更新日志

  • fix: 修复 store 中并未对 localStorage 的 TOKEN_NAME 键进行赋值的缺陷

  • 本条 PR 不需要纳入 Changelog

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

@github-actions
Copy link
Contributor

github-actions bot commented May 12, 2023

完成

@yixiaco
Copy link

yixiaco commented May 12, 2023

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪
参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

@SpringHgui
Copy link
Contributor Author

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪 参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

也许一开始没做持久化处理,那时token手动存localStorage

@yixiaco
Copy link

yixiaco commented May 12, 2023

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪 参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

也许一开始没做持久化处理,那时token手动存localStorage

image
我有一个参考配置(nuxt3环境需要使用cookie,这里不需要),也许只需要缓存token,其他信息从接口中获取,因为缓存用户信息是不合理的,role并不会一直不变,肯定是从接口中获取到的。

@SpringHgui
Copy link
Contributor Author

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪 参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

也许一开始没做持久化处理,那时token手动存localStorage

image 我有一个参考配置(nuxt3环境需要使用cookie,这里不需要),也许只需要缓存token,其他信息从接口中获取,因为缓存用户信息是不合理的,role并不会一直不变,肯定是从接口中获取到的。

你是说删除手动存储localStorage的逻辑,改成通过persist插件负责存储持久化token,axios拦截器中从pinia中获取?

@SpringHgui
Copy link
Contributor Author

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪 参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

也许一开始没做持久化处理,那时token手动存localStorage

image 我有一个参考配置(nuxt3环境需要使用cookie,这里不需要),也许只需要缓存token,其他信息从接口中获取,因为缓存用户信息是不合理的,role并不会一直不变,肯定是从接口中获取到的。

你是说删除手动存储localStorage的逻辑,改成通过persist插件负责存储持久化token,axios拦截器中的token改成从pinia中获取?

@SpringHgui
Copy link
Contributor Author

@uyarn 有空了麻烦看下这个token没有携带后端的问题,还有withCredentials是不是应该默认关闭掉

@yixiaco
Copy link

yixiaco commented May 15, 2023

持久化用的应该是pinia-plugin-persistedstate插件,开启persist之后会对值默认保存到localStorage中,但为什么没有指定TOKEN_NAME确实有一点奇怪 参考链接 https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html

也许一开始没做持久化处理,那时token手动存localStorage

image 我有一个参考配置(nuxt3环境需要使用cookie,这里不需要),也许只需要缓存token,其他信息从接口中获取,因为缓存用户信息是不合理的,role并不会一直不变,肯定是从接口中获取到的。

你是说删除手动存储localStorage的逻辑,改成通过persist插件负责存储持久化token,axios拦截器中从pinia中获取?

建议而已,其实两种写法都没问题。只是说使用persist插件改造成SSR会更方便一点。

@SpringHgui SpringHgui changed the title 接口请求不携带token,是否可以PR? 接口请求不携带token May 15, 2023
@uyarn
Copy link
Collaborator

uyarn commented May 18, 2023

第一个问题没处理好 agree 第二个问题 我觉得很难定义吧 暂时不改动了

@uyarn uyarn changed the title 接口请求不携带token fix: 修复 store 中并未对 localStorage 的 TOKEN_NAME 键进行赋值的缺陷 May 18, 2023
@uyarn uyarn merged commit 665e0da into Tencent:develop May 18, 2023
@yixiaco
Copy link

yixiaco commented May 18, 2023

第一个问题没处理好 agree 第二个问题 我觉得很难定义吧 暂时不改动了

@uyarn 我觉得他的key和paths定义写反了
image
token变量为什么定义成动态的,很不理解

uyarn added a commit that referenced this pull request May 18, 2023
* perf: 路由守卫获取菜单异常,跳转登录页并弹窗提示 (#502)

* perf: 规范不同系统中的结束符 (#505)

* fix: eol

* fix: 测试效果

* fix: 默认全部文件eol=lf

* fix: 防止误判,仅对部分明确的文件设置

* fix: 新建文件eol格式错误进行互补

* Update settings.json

* fix: 修复无法将通知设为未读 (#511)

* fix: 修复 store 中并未对 localStorage 的 TOKEN_NAME 键进行赋值的缺陷 (#504)

* fix: 接口请求不携带token

* fix: 优化token持久化策略

* fix: 删除多余的log

* chore: release 0.7.5 (#512)

---------

Co-authored-by: liweijie0812 <674416404@qq.com>
Co-authored-by: Gui.H <springhgui@outlook.com>
Co-authored-by: You <i@izoyo.cn>
PDieE added a commit to PDieE/tdesign-vue-next-starter that referenced this pull request May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants