JWT 令牌未保存在数据库中

di0*_*i0n 6 authentication jwt adonis.js

用户认证返回token,但不保存在数据库中

验证控制器:

'use strict'

const User = use("App/Models/User");

class AuthController {
  async registrar({ request }) {
    const data = request.only(["username", "email", "password"]);

    const user = await User.create(data);

    return user;
  }

  async autenticar({ request, auth }) {
    const { email, password } = request.all();
    const retorno = {};
    let token;
    if (token = await auth.attempt(email, password)) {
      const user = await User.findBy('email', email)
      retorno.token = token.token;
      retorno.user = user.username;
      retorno.id = user.id;
    } else {
      retorno.data = "E-mail ou senha Incorretos";
    }
    return retorno;
  }

}

module.exports = AuthController
Run Code Online (Sandbox Code Playgroud)

我的请求

POST http://localhost:3333/autenticar
{
    "email":"c@gmail.com",
    "password": "123456"
}
Run Code Online (Sandbox Code Playgroud)

我的回复

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEsImlhdCI6MTYxNTI5Njk4MH0.O0X4gGIEtMiZfEH3VxWbffsCZQDUhgEv0CymA0dB6z8",
  "user": "gui",
  "id": 1
}
Run Code Online (Sandbox Code Playgroud)

请求和响应授权

请求后我的令牌表 0 个令牌

我在另一个网站上发现了同样的问题,但没有找到有帮助的答案。

crb*_*ast 4

AdonisJS 不在数据库中存储JWT令牌。仅存储刷新令牌。


为什么 JWT 令牌不被存储?

^ JWT 未保存在数据库中,因为它没有用。所有 JWT 令牌都经过签名,因此服务器可以轻松检查令牌是否有效。有用的答案我应该在哪里存储 jwt 令牌以在服务器端进行身份验证

JWT 令牌不像不透明令牌那样工作。不透明令牌保存在数据库中,后端检查令牌是否存在,然后授予访问权限。

有用的链接:https://medium.com/@piyumimdasanayaka/json-web-token-jwt-vs-opaque-token-984791a3e715


了解 JSON Web 令牌: