Skip to content

Instantly share code, notes, and snippets.

@vaygeth89
Last active October 13, 2021 03:15
Show Gist options
  • Save vaygeth89/45e27c48d5ba9e29c1063c2e7adeb101 to your computer and use it in GitHub Desktop.
Save vaygeth89/45e27c48d5ba9e29c1063c2e7adeb101 to your computer and use it in GitHub Desktop.
import 'package:tutorial_flutter_minimalist_authentication/exceptions/access_token_exception.dart';
import 'package:tutorial_flutter_minimalist_authentication/models/account/renew_access_token.dart';
import 'package:tutorial_flutter_minimalist_authentication/models/account/sign_in.dart';
import 'package:tutorial_flutter_minimalist_authentication/models/api/business_error.dart';
import 'package:tutorial_flutter_minimalist_authentication/models/session/authentication_data.dart';
import 'package:tutorial_flutter_minimalist_authentication/repositories/api/account_repository.dart';
class FakeAccountAPIRepository implements AccountRepository {
final _fakeAuthData = AuthenticationData(
accessToken: "fakeAccessToken",
refreshToken: "fakeRefreshToken",
id: "1");
final _fakeEmail = "[email protected]";
final _fakePassword = "password";
@override
Future<AuthenticationData> signIn({
required SignIn signIn,
}) async {
try {
if (signIn.email == _fakeEmail && signIn.password == _fakePassword) {
return _fakeAuthData;
}
final businessCode = 123;
//you make your API return special business code for different error
//for example if business code is 123 when user try to sign in and it was invalid credentials
//or for example if business code is 321 when user sign up before but didn't verify his email
throw BusinessError(message: "Invalid Sign In", businessError: 123);
} on BusinessError catch (error) {
throw error;
} catch (error) {
throw Exception();
}
}
@override
Future<AuthenticationData> renewAccessToken(
{required RenewAccessToken renewAccessToken}) async {
return _fakeAuthData;
}
@override
Future<bool> verifyToken({required String accessToken}) async {
try {
return true;
} on AccessTokenException catch (error) {
//Here you can throw AccessTokenException if access token either expired or invalid
throw error;
} catch (error) {
throw error;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment