Skip to content

Commit

Permalink
Better auth handling, logging enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
NamanShergill committed Mar 24, 2021
1 parent 31577ff commit 391f1fa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
34 changes: 18 additions & 16 deletions lib/app/Dio/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GetDio {
String baseURL = Global.apiBaseURL,
bool applyBaseURL = true,
bool loginRequired = true,
bool debugLog = false,
bool debugLog = true,
bool buttonLock = true,
bool showPopup = true,
String? acceptHeader,
Expand Down Expand Up @@ -57,17 +57,19 @@ class GetDio {

// Check cache first and return cached data if supplied maxAge
// has not elapsed.
final key = CacheOptions.defaultCacheKeyBuilder(options);
final cache = await Global.cacheStore.get(key);
if (cache != null &&
cacheOptions != null &&
!(cacheOptions.policy == CachePolicy.refresh) &&
DateTime.now()
.isBefore(cache.responseDate.add(cacheOptions.maxAge))) {
if (buttonLock) ButtonController.setButtonValue(false);
// Resolve the request and pass cached data as response.
return handler
.resolve(cache.toResponse(options, fromNetwork: false));
if (cacheEnabled) {
final key = CacheOptions.defaultCacheKeyBuilder(options);
final cache = await Global.cacheStore.get(key);
if (cache != null &&
cacheOptions != null &&
!(cacheOptions.policy == CachePolicy.refresh) &&
DateTime.now()
.isBefore(cache.responseDate.add(cacheOptions.maxAge))) {
if (buttonLock) ButtonController.setButtonValue(false);
// Resolve the request and pass cached data as response.
return handler
.resolve(cache.toResponse(options, fromNetwork: false));
}
}
// Proceed with the request.
handler.next(options);
Expand All @@ -92,7 +94,7 @@ class GetDio {

// Todo: Add better exception handling based on response codes.
if (error.response == null) {
throw Exception(error.message);
handler.next(error);
} else if (error.response?.data.runtimeType is Map &&
error.response?.data.containsKey("message") &&
showPopup) {
Expand All @@ -109,12 +111,12 @@ class GetDio {
DioCacheInterceptor(
options: cacheOptions ??
CacheOptions(
policy: CachePolicy.request, store: Global.cacheStore)),
policy: CachePolicy.refresh, store: Global.cacheStore)),
);
// Log the request in the console for debugging if [debugLog] is true.
if (debugLog)
dio.interceptors
.add(PrettyDioLogger(requestHeader: true, requestBody: true));
dio.interceptors.add(PrettyDioLogger(
requestHeader: true, requestBody: true, responseHeader: true));
return dio;
}
}
18 changes: 11 additions & 7 deletions lib/blocs/authentication_bloc/authentication_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ class AuthenticationBloc
yield AuthenticationUnauthenticated();
} else if (event is RequestDeviceCode) {
// Get device code to initiate authentication.
Response response = await AuthService.getDeviceToken();
// ['device_code'] should not be null.
if (response.data['device_code'] != null) {
yield AuthenticationInitialized(
DeviceCodeModel.fromJson(response.data));
} else {
yield AuthenticationError('Something went wrong, please try again.');
try {
Response response = await AuthService.getDeviceToken();
// ['device_code'] should not be null.
if (response.data['device_code'] != null) {
yield AuthenticationInitialized(
DeviceCodeModel.fromJson(response.data));
} else {
yield AuthenticationError('Something went wrong, please try again.');
}
} catch (e) {
add(AuthError(e.toString()));
}
} else if (event is RequestAccessToken) {
// Recurring function to request access token from Github on the supplied interval
Expand Down
31 changes: 20 additions & 11 deletions lib/services/authentication/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AuthService {
var response = await GetDio.getDio(
loggedIn: false,
baseURL: 'https://github.com/',
debugLog: false,
loginRequired: false)
.post("${_url}device/code", data: formData);
return response;
Expand All @@ -67,19 +68,27 @@ class AuthService {
'device_code': deviceCode,
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
});
Response response = await GetDio.getDio(
loggedIn: false,
loginRequired: false,
baseURL: 'https://github.com/',
buttonLock: false)
.post("${_url}oauth/access_token", data: formData);
if (response.data['access_token'] != null) {
storeAccessToken(AccessTokenModel.fromJson(response.data));
try {
Response response = await GetDio.getDio(
loggedIn: false,
loginRequired: false,
cacheEnabled: false,
debugLog: false,
baseURL: 'https://github.com/',
buttonLock: false)
.post("${_url}oauth/access_token", data: formData);
if (response.data['access_token'] != null) {
storeAccessToken(AccessTokenModel.fromJson(response.data));
return response;
} else if (response.data['error'] != null &&
response.data['error'] != "authorization_pending" &&
response.data['error'] != "slow_down")
throw Exception(response.data['error_description']);
return response;
} else if (response.data['error'] == 'incorrect_device_code') {
throw Exception(response.data['error_description']);
} catch (e) {
print(e);
throw Exception(e);
}
return response;
}

Future<DeviceCodeModel> getDeviceCode() async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A Github mobile client

publish_to: 'none'

version: 0.0.2-24032021.internal+2
version: 0.0.3-25032021.internal+3

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down

0 comments on commit 391f1fa

Please sign in to comment.