-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
如何做全局的登录状态管理 #95
Comments
我建议采用第二种(使用请求控制),很简单,封装一下你的 request 函数,然后在里面根据情况处理即可(跳转页面,弹框提示之类的) |
在 subscription 里处理:
|
我要在每个请求service中带上登录后的token,请问这种情况怎么写比较好? |
@rrandom 我在 fetch 外又封装了一个 $http,在这个里面加 token |
@hbrls 请问你的token是存在store里的吗? 如果是的话,请问你的$http是怎么写的 |
token 存 cookie 里的吧,$http 的实现我觉得应该类似 https://github.com/dvajs/dva-cli/blob/master/boilerplates/app/src/utils/request.js |
@sorrycc 请问在dva中,怎么把token设置到cookie中? |
token 设到 cookie 里不是 dva 做的事情,是服务器端响应头做的。 |
@sorrycc 在dora中有没有办法?我现在的做法是所有api会在网站服务器端加上token。昨天才发现dva和dora,挺不错。 |
我两种都做了. |
可以放在localstorage 用jsonwebtoken即可 |
用户登录的信息 可以保存在 login_model (相当于全局的model),那么在其他的model 怎么取到这个login_model的信息呢 |
这个我找到了 方法 yield select(global => global) |
|
@sorrycc 我是在onEnter 中 处理登录问题的,请问在onEnter中(或者在router中)如何获取store或者dispatch呢? |
onEnter 是在 router.js 里吗? 是在话可以先通过 app._store.dispatch 处理吧。 app.router(({ history, app }) => {}); 这里可以获取到 app 实例的,https://github.com/dvajs/dva/blob/4cca243/index.d.ts#L70 。 |
@sorrycc onEnter 是在 router.js 里 已经用上面的方法成功获取dispatch。多谢。 |
@sorrycc 如何在 request.js 中获取 dispatch |
我是封装了一层 request.js 用户登录后服务端将Token传回来保存在 localstorage,fetch前将token放到cookie中,服务端验证token,如果token验证失败,后端返回401 在request.js response方法中统一错误处理 抛出 Error 在 index里捕获相应错误,然后用 window.location='/login' 跳转登陆页 |
@liSong5713 请教一下,在其他的model获取全局的model中的数据要怎么写,可以给个示例吗? |
我在我的项目里,两种保持登录状态的方式都尝试了: |
@zhanbohui 也是一样 effect中 yield select(globalState=>state}) |
一定要使用dva吗? |
@Vcgoyo 页面没有发送请求,就是说还是收不到401,这个时候通过什么判断用户是否登录 |
检查是否登录:
然后在代码里如何控制跳转呢?
The text was updated successfully, but these errors were encountered: