JWT token not saved on database

Viewed 675

A user's authentication returns the token, but it is not saved in the database

AuthController:

'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

My Request

POST http://localhost:3333/autenticar
{
    "email":"c@gmail.com",
    "password": "123456"
}

My Response

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEsImlhdCI6MTYxNTI5Njk4MH0.O0X4gGIEtMiZfEH3VxWbffsCZQDUhgEv0CymA0dB6z8",
  "user": "gui",
  "id": 1
}

request and response auth

My tokens table after the request 0 tokens

I found the same question on another site, but I didn't have an answer that would help.

1 Answers

AdonisJS don't store JWT token in the db. Only refresh token are stored.


Why JWT token are not stored?

^ JWT are not saved on database because it's not useful. All JWT tokens are signed so the server can easily check if token is valid. Useful answer Where should I store jwt token for authentication on server side

JWT token not works like opaque token. Opaque token are saved on the database and the backend check if the token exist and then grant access.

Useful link : https://medium.com/@piyumimdasanayaka/json-web-token-jwt-vs-opaque-token-984791a3e715


Learn about JSON Web Token :

Related