authentication - Guardian.Plug.current_resource(conn) 返回 nil
问题描述
我正在将 Guardian 从 更新v0.14
为v1.0
。
我正在关注官方监护人 github 页面的更新指南,但遇到了一个问题。
为了更新,我稍微改变了我的身份验证逻辑。
从:
case Myapp.Session.authenticate(session_params) do
{:ok, user} ->
{:ok, jwt, _full_claims} = user |> Guardian.encode_and_sign(:token)
conn
|> put_status(:created)
|> render("show.json", jwt: jwt, user: user)
:error ->
conn
|> put_status(:unprocessable_entity)
|> render("error.json")
end
至:
case Myapp.Session.authenticate(session_params) do
{:ok, user} ->
{:ok, jwt, _full_claims} = Core.Guardian.encode_and_sign(user)
conn
|> put_status(:created)
|> render("show.json", jwt: jwt, user: user)
:error ->
conn
|> put_status(:unprocessable_entity)
|> render("error.json")
end
而我nil
从Guardian.Plug.current_resource(conn)
……为了解决这个问题,我存储token
在前端localStorage
并通过它来获取用户信息。
但我仍然认为从current_resource
. 我怎样才能做到这一点?
- 编辑
current_user_controller.ex
def show(conn, _) do
user = Myapp.Guardian.Plug.current_resource(conn)
conn
|> put_status(:ok)
|> render("show.json", user: user)
end
解决方案
推荐阅读
- reactjs - react-router-dom 可能在一条路线中具有“oneof”参数
- elasticsearch - Elasticsearch 字段以最小的内部点击数崩溃
- python-3.x - 在 django 框架上为个人资料用户上传图片
- c# - 一个类的 2 个泛型类型不能相同的约束
- sql-server - 无法使用 Sqoop 将 SQL Server 视图中的所有记录导入 HDFS
- mysql - 如何获取 MySQL 中两列范围之间的记录?
- c# - 如何刷新DataGridView中只有一个单元格的背景颜色
- swift - 如何模拟 DataTaskPublisher?
- angular - 如何制作拦截器单元测试
- python - 线性回归 - 不正确的输出