Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter web - Logout when status code is 401 #1181

Closed
tomasweigenast opened this issue Jun 2, 2021 · 1 comment
Closed

Flutter web - Logout when status code is 401 #1181

tomasweigenast opened this issue Jun 2, 2021 · 1 comment

Comments

@tomasweigenast
Copy link

I'm trying to implement an interceptor which handles responses with 401 status code. If a response ends with that status code, it will clear the current queue, logout and then, navigate to a login view.

@override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if(response.statusCode == HttpStatus.unauthorized) {
      Logger.debug('Unauthorized error. Logging user out...');
      _dio.clear();

      // Logout
      ServiceProvider.getService<BaseAccountWorker>().logout().then((value) {
        // Navigate to login
        App.navigator?.navigate(LoginView.route);
        Logger.debug('Logged out.');
      }).catchError((err) {
        print(err);
      });

      handler.reject(DioError(
        requestOptions: response.requestOptions,
        error: "Unauthorized",
        type: DioErrorType.response,
        response: null
      ));

    }

    return super.onResponse(response, handler);
  }

The problem is that I run into a loop when executing that code, it never ends. Always starting from top.

@stale
Copy link

stale bot commented Jul 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Jul 8, 2021
@stale stale bot closed this as completed Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant