Skip to content

Commit

Permalink
- add AuthOptions instead of method parameters.
Browse files Browse the repository at this point in the history
- add interceptors for authentication to handle different authentication uses.
- bump to BloC 6.0.0
- add helper classes for pagination and error responses.
  • Loading branch information
mustafa96m committed Jul 22, 2020
1 parent e67b635 commit 0d47c65
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 149 deletions.
6 changes: 4 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:miswag_auth/bloc/bloc.dart';
import 'package:miswag_auth/miswag_auth.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:miswag_auth/models/auth_options.dart';

main() {
Authentication.setup(
const AuthOptions options = AuthOptions(
baseUrl: "https://jsonbin.io/", loginPath: "5ebcfb3a47a2266b14784142");
Authentication.setup(options: options);
runApp(SampleApp());
}

Expand Down
100 changes: 78 additions & 22 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ packages:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
version: "2.0.13"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
version: "1.6.0"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
version: "2.4.1"
bloc:
dependency: transitive
description:
name: bloc
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
version: "6.0.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
version: "2.0.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
version: "1.1.3"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
version: "1.14.12"
convert:
dependency: transitive
description:
Expand All @@ -63,7 +63,7 @@ packages:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
version: "2.1.4"
dio:
dependency: transitive
description:
Expand All @@ -77,7 +77,14 @@ packages:
name: equatable
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
version: "1.2.2"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "5.2.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -89,7 +96,7 @@ packages:
name: flutter_bloc
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
version: "6.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -120,7 +127,14 @@ packages:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
version: "2.1.12"
intl:
dependency: transitive
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -156,41 +170,76 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
pedantic:
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+2"
path_provider_platform_interface:
dependency: transitive
description:
name: pedantic
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
version: "1.0.2"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.13"
provider:
dependency: transitive
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.5+1"
version: "4.3.1"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
version: "2.1.3"
shared_preferences:
dependency: transitive
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.7+2"
version: "0.5.8"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.2+1"
shared_preferences_macos:
dependency: transitive
description:
Expand Down Expand Up @@ -223,7 +272,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
version: "1.7.0"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -258,7 +307,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.11"
version: "0.2.15"
typed_data:
dependency: transitive
description:
Expand All @@ -273,13 +322,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
version: "3.6.1"
sdks:
dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.16.0 <2.0.0"
8 changes: 4 additions & 4 deletions lib/bloc/authentication_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';
import 'package:miswag_auth/repository/user_repository.dart';

import 'bloc.dart';

class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
final UserRepository userRepository;

AuthenticationBloc({@required this.userRepository})
: assert(userRepository != null);

@override
AuthenticationState get initialState => AuthenticationUninitialized();
: assert(userRepository != null),
super(AuthenticationUninitialized());

@override
Stream<AuthenticationState> mapEventToState(
Expand Down
50 changes: 18 additions & 32 deletions lib/misc/client/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:miswag_auth/misc/client/interceptors/authentication_interceptor.dart';
import 'package:miswag_auth/misc/client/interceptors/debug_interceptor.dart';
import 'package:miswag_auth/models/auth_options.dart';

class ApiClient {
final String baseUrl;
final String tokenKey;

final AuthOptions authOptions;
final bool debug;
Map<String, Object> authentication;

Dio client;
Dio authClient;

ApiClient({
@required this.baseUrl,
this.tokenKey = "token",
}) : assert(baseUrl != null) {
ApiClient(this.authOptions, {this.debug = false})
: assert(authOptions != null) {
final BaseOptions options = BaseOptions(
baseUrl: baseUrl,
connectTimeout: 5000,
receiveTimeout: 3000,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
);
baseUrl: authOptions.baseUrl,
connectTimeout: 5000,
receiveTimeout: 3000,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
});
authClient = Dio(options);
client = Dio(options);
client.interceptors.add(InterceptorsWrapper(
onRequest: (RequestOptions options) async {
if (authentication != null) {
options.queryParameters.addAll({
tokenKey: authentication[tokenKey]
});
}
return options;
}));
client.interceptors.add(AuthenticationInterceptors(client: this));
if (debug) {
client.interceptors.add(DebugInterceptor());
}
}


Future<Response> responseInterceptor(Response response) async {
if (authentication != null) {}
return response;
}

}
43 changes: 43 additions & 0 deletions lib/misc/client/interceptors/authentication_interceptor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:dio/dio.dart';
import 'package:miswag_auth/misc/client/api_client.dart';
import 'package:miswag_auth/models/auth_options.dart';

class AuthenticationInterceptors extends InterceptorsWrapper {
final ApiClient client;

AuthenticationInterceptors({this.client}) : assert(client != null);

@override
Future onRequest(RequestOptions options) {
if (this.client.authentication != null) {
injectToken(options);
}
return super.onRequest(options);
}

@override
Future onResponse(Response response) {
return super.onResponse(response);
}

@override
Future onError(DioError err) {
return super.onError(err);
}

void injectToken(RequestOptions options) {
if (this.client.authOptions.tokenType == TokenType.GET) {
options.queryParameters.addAll({
this.client.authOptions.tokenKey:
this.client.authentication[this.client.authOptions.tokenKey]
});
}

if (this.client.authOptions.tokenType == TokenType.Bearer) {
options.headers.addAll({
this.client.authOptions.customAuthHeader:
"${this.client.authOptions.tokenType} ${this.client.authentication[this.client.authOptions.tokenKey]}"
});
}
}
}
Loading

0 comments on commit 0d47c65

Please sign in to comment.