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

Automatic management of tokens for API in general #34

Open
wants to merge 7 commits into
base: feed-test
Choose a base branch
from

Conversation

jdubpark
Copy link

@jdubpark jdubpark commented Aug 6, 2021

Using Dio, I add interceptors to attach access token to headers & request token to the cookie (using CookieManager). Also created request clients to make requesting easy -- divided into two client types: getClient and getClientWithAuth.

getClient is a normal request client, no authorization.
getClientWithAuth is a request with cookie & header pre-attached.

Both clients' response is parsed and checked for new access token.

Comment on lines +40 to +64
Future<Dio> getClient() async {
final Dio _dio = Dio();

_dio.options.baseUrl = '$_baseUrl/api/v1';
_dio.interceptors.clear();

_dio.interceptors.add(InterceptorsWrapper(
onResponse: (Response response, handler) async {
var data = response.data;

if (data['error'] != null) {
log(data.toString());
throw ApiException(data['error']);
}

// save access token (refresh token is auto-saved by CookieManager above)
if (data['payload']?['accessToken'] != null && data['payload']?['accessToken'] != '') {
await storage.write(key: 'jwt_access', value: data['payload']?['accessToken']);
}

return data;
},
));

return _dio;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should just go with getClientWithAuth alone, since it's probably good to make all our API requests authenticated even if not yet necessary (maybe validation is added later)?

Copy link
Member

@AndrewLester AndrewLester Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think there's a way to extend the Dio client, which would make ApiService itself available for running gets, posts, etc. with the credentials automatically applied. See here: https://pub.dev/packages/dio#extends-dio-class

Not sure if this will do what we need it do though, as the docs aren't good in this section. It may allow for the removal of the getClient method though, which I think makes things clearer when the code is used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to use just getClientWithAuth, then we need to modify it to allow request even if there is no access token (for /section/search and /course/search). Extending the Dio class would be nice, but I don't see the need for it at the moment. If you are able to do it without much hassle, sure go for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we extend the class, we could pass an additional option (part of options) to distinguish between auth & no-auth requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah extending the Dio class may be as much of a hassle as extending IOClient is. Does running a /section/search with some query parameters and an access token cause an error?

If it does, then I suppose we will need to have separate code paths for authenticated/unauthenticated requests. I propose changing the method getClient to use a keyword argument credentials or auth or something that can be true or false.

@rahulshamkuwar
Copy link
Contributor

Should I close this pr since we're probably going to use the api-test?

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

Successfully merging this pull request may close these issues.

3 participants