Skip to content

"Error: next() called multiple times" when using doRefresh #5

Description

@acepukas

I've been attempting to use the refresh token flow via the doRefresh function but I get the error

Error: next() called multiple times

After some testing I was able to get it working only if I removed the await next() call on main.js, line 57.

...
    } catch (e) {
      if (e.message === "jwt expired" && doRefresh) {
        try {
          const refreshToken = fromCookies(ctx, opts, true);
          const accessToken = extractToken(ctx, opts);
          const decodedAccessToken = await verifyAsync(accessToken, secret, { ignoreExpiration: true });
          await doRefresh(ctx, opts, refreshToken, decodedAccessToken);
          ctx.state = ctx.state || {};
          ctx.state[key] = decodedAccessToken;
        } catch (e) {
          ctx.throw(401, `Invalid token - ${e.message}`);
        }
        await next();  // <-- Is this call necessary? Line 57
      } else ctx.throw(401, `Invalid token - ${e.message}`);
    }
    await next();
  }
...

It seems that only the call to next() on line 60 is necessary as all logic will flow to that point unless an exception is thrown. If both calls are present, the above error happens as middleware unwinds back down the stack.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions