supertokens/supertokens-flutter

Dio adaptation for the SDK

Closed this issue · 1 comments

Unlike the http library dio allows us to use interceptors, hence that's what we'll be leveraging to implement out SDk.

Dio addInterceptors(Dio dio) {
  return dio..interceptors.add(InterceptorsWrapper(
      onRequest: (RequestOptions options) => requestInterceptor(options),
      onResponse: (Response response) => responseInterceptor(response),
      onError: (DioError dioError) => errorInterceptor(dioError)));
}

This is how we add interceptors to dio, so these callbacks onRequest, onResponse and onError can be exported from the Supertokens class.

Then the above will look something like this:

Dio addInterceptors(Dio dio) {
  return dio..interceptors.add(SuperTokensInterceptorWrapper());
}

apart from this we tend to make the cookieStore a singleton and share this across both the methods this helps us in a major way because we already have a lot of functions already written in http hence by sharing the cookieStore we can only expose interceptors to do the checks and modifying things to cookie but the signOut, refresh and other internet functions can still be done by using the http based functions.

On further analysis we have decided not update onError hook, rather we whitelist the Supertokens.sessionExpiryStatusCode as a valid response and then handle the corresponding in the onResponse hook. This approach was taken make the flow of compatible with other interceptors