Adonis jwt authentication with refresh token


I’m trying to setup authentication with JWT token and refresh token, for rest api app. Now, I got it working to issue access_token and refresh_token, but I have troubles making refresh_token work.

When access_token is expired, I send the refresh token back, and I don’t get new access_token.

So my question is: should it be handled automatically (accepting refresh token and issuing new access token), and if not, where do I need to write logic for that ?

Naturally I assumed middleware and tried something like this:

'use strict';

class AuthRefresh {
  async handle({ request, auth }, next) {
    // call next to advance the request
    // const refreshToken = request.input('refresh_token');
    const refreshToken = request.header('refresh_token');

    const newAccessToken = await auth.generateForRefreshToken(refreshToken);
    await next(newAccessToken);

module.exports = AuthRefresh;

but its not working.

Help is appreciated.



Please have a look at this.


I solved it with this code:

    const { refresh_token, email, password } = request.all();
    if (refresh_token) {
      return await auth
        // .newRefreshToken()

    return await auth.withRefreshToken().attempt(email, password);