Skip to content

Commit

Permalink
Merge pull request #6 from kinde-oss/feature/sdk_reflect
Browse files Browse the repository at this point in the history
+ Fix bug: can't load url with https
  • Loading branch information
onderay authored Nov 5, 2023
2 parents 78f7090 + 06fb3dd commit 96cf3e0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ files:
destinationFilename: lib/src/kinde_error.dart
lib/src/kinde_flutter_sdk.dart:
destinationFilename: lib/src/kinde_flutter_sdk.dart
lib/src/handle_network_error_mixin.dart:
destinationFilename: lib/src/handle_network_error_mixin.dart
lib/src/keys/keys.dart:
destinationFilename: lib/src/keys/keys.dart
lib/src/keys/keys_api.dart:
Expand Down
33 changes: 33 additions & 0 deletions my_custom_templates/lib/src/handle_network_error_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:dio/dio.dart';
import 'package:kinde_flutter_sdk/src/kinde_error.dart';

mixin HandleNetworkMixin {
Exception handleError(Exception error) {
if (error is KindeError) {
return error;
}
if (error is DioException) {
DioException dioError = error;
switch (dioError.type) {
case DioExceptionType.cancel:
case DioExceptionType.connectionTimeout:
case DioExceptionType.receiveTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.connectionError:
case DioExceptionType.badCertificate:
case DioExceptionType.unknown:
if (dioError.error is KindeError) {
return dioError.error as KindeError;
}
return KindeError(dioError.message ?? "");
case DioExceptionType.badResponse:
if (dioError.requestOptions.path == "/oauth2/token") {
return KindeError("Refresh token Expired");
}
return KindeError(dioError.message ?? "");
}
} else {
return error;
}
}
}
2 changes: 1 addition & 1 deletion my_custom_templates/lib/src/kinde_error.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class KindeError extends Error {
class KindeError implements Exception {
final String message;

KindeError(this.message);
Expand Down
31 changes: 22 additions & 9 deletions my_custom_templates/lib/src/kinde_flutter_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ as secure_store;
import 'package:hive/hive.dart';
import 'package:jose/jose.dart';
import 'package:kinde_flutter_sdk/kinde_flutter_sdk.dart';
import 'package:kinde_flutter_sdk/src/handle_network_error_mixin.dart';
import 'package:kinde_flutter_sdk/src/keys/keys_api.dart';
import 'package:kinde_flutter_sdk/src/kinde_error.dart';
import 'package:kinde_flutter_sdk/src/store/store.dart';
Expand All @@ -23,7 +24,7 @@ import 'package:kinde_flutter_sdk/src/token/token_utils.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pubspec_parse/pubspec_parse.dart';

class KindeFlutterSDK with TokenUtils {
class KindeFlutterSDK with TokenUtils, HandleNetworkMixin {
static const _orgCodeParamName = 'org_code';
static const _orgNameParamName = 'org_name';
static const _audienceParamName = 'audience';
Expand Down Expand Up @@ -60,15 +61,23 @@ class KindeFlutterSDK with TokenUtils {
if (_config == null) {
throw KindeError('KindeFlutterSDK have not been configured');
}

var domainUrl = "";
if (_config!.authDomain.startsWith('https')) {
domainUrl = _config!.authDomain;
} else if (_config!.authDomain.startsWith('http')) {
domainUrl = _config!.authDomain.replaceFirst('http', "https");
} else {
domainUrl = 'https://${_config!.authDomain}';
}

_serviceConfiguration = AuthorizationServiceConfiguration(
authorizationEndpoint: 'https://${_config!.authDomain}$_authPath',
tokenEndpoint: 'https://${_config!.authDomain}$_tokenPath',
endSessionEndpoint: 'https://${_config!.authDomain}$_logoutPath');
authorizationEndpoint: '$domainUrl$_authPath',
tokenEndpoint: '$domainUrl$_tokenPath',
endSessionEndpoint: '$domainUrl$_logoutPath');

Dio dio = Dio(BaseOptions(
baseUrl: 'https://${_config!.authDomain}',
connectTimeout: const Duration(milliseconds: 5000),
receiveTimeout: const Duration(milliseconds: 3000),
baseUrl: domainUrl,
));

_kindeApi = KindeApi(dio: dio, interceptors: [
Expand Down Expand Up @@ -181,12 +190,16 @@ class KindeFlutterSDK with TokenUtils {
Future<UserProfileV2?> getUserProfileV2() async {
return _kindeApi.getOAuthApi().getUserProfileV2().then((value) {
return value.data;
}).catchError((error) {
throw handleError(error);
});
}

Future<UserProfile?> getUser() async {
return _kindeApi.getOAuthApi().getUser().then((value) {
return value.data;
}).catchError((error) {
throw handleError(error);
});
}

Expand Down Expand Up @@ -217,8 +230,8 @@ class KindeFlutterSDK with TokenUtils {
return _store.authState?.accessToken;
} on KindeError catch (_) {
rethrow;
} catch (ex) {
return null;
} on Exception catch (ex) {
throw handleError(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ class RefreshTokenInterceptor<T> extends InterceptorsWrapper {
if (err.response?.statusCode == HttpStatus.forbidden) {
try {
// If a 403 response is received, refresh the access token
var newToken = await refreshToken();

if(newToken == null) {
throw KindeError("Refresh token Expired");
}
await refreshToken();

// Repeat the request with the updated header
return handler.resolve(await dio.fetch(err.requestOptions));
Expand Down

0 comments on commit 96cf3e0

Please sign in to comment.