I am working with flutter bloc in my project which is working great.
I have an API service class which uses Dio for making API requests. I am using an Interceptors for handling error. What I am trying to achieve is whenever API throws 401 error I want to logout the user and show login page. I will also be invalidating the token.
So my question is, If I want to logout the user as soon as 401 occur ie. from API service class do I call logout event which is defined in the Authentication Bloc. To do so I will have to have instance of Authentication Bloc in API Service class. Is this approach correct? or is there any other simpler ways to achieve this.
class APIService{
...
onError: (DioError error, handler) async {
if (error.response?.statusCode == 401){
//invalidate the token
//Do I call the logout event of authentication bloc here?
}
}
...
}