用户登录时使用 dj-rest-auth 获取身份验证令牌

时间:2021-06-18 07:49:23

标签: django django-rest-framework django-rest-auth auth-token

以前我使用的是 django-rest-auth 包,当用户 auth token 响应 log in 和这个 response.data.keyauth token 时,我得到了 key api 调用作为 authentication auth

工作正常

以前用于 django-rest-auth

"/rest-auth/login/"

正在将 response.data.key 设为 auth token 并且正在运行 我已经保存在cookie中以备后用

.get("/rest-auth/user/", {
     headers: {
       "Content-Type": "application/json",
       Authorization: "Token " + response.data.key + "",
       },
  })

它正在努力获取有关用户的信息,并且通过将其设置为在其他 api 调用中使用时也起作用

Authorization: "Token " + response.data.key + ""

但现在,我使用 dj-rest-auth 包而不是 django-rest-auth 并将我的网址转移到

/dj-rest-auth/login

而且我没有得到任何可用于标题中的 keyauth tokenauthorization

.get("/dj-rest-auth/user/", {
     headers: {
       "Content-Type": "application/json",
       Authorization: "Token " + response.data.? + "",
       },
  })

它不起作用,因为现在我收到了 access_tokenrefresh_tokenuser info。我尝试在标头中将 access_tokenrefresh_token 用于 authorization 但它不起作用,因为当用户 {{ 1}}

注意:key 不再维护,auth token 是从前一个分叉出来的,有更多的功能(这就是我切换的原因)

如何使用 log in 包获取 django-rest-authdj-rest-auth,以便我可以在 API 调用的 auth token 中使用它进行授权?

1 个答案:

答案 0 :(得分:1)

检查您的设置中是否没有REST_USE_JWT = True。该设置将启用 JWT 身份验证方案,而不是(默认)基于令牌的。