Skip to content

Commit

Permalink
refactor!: AuthCubit now extends HydratedAuthCubitBase
Browse files Browse the repository at this point in the history
- updated docs
**Breaking changes**
- authenticateWithSocialProvider -> requestTokenFromSocialProvider
- logoutWithSocialProvider -> logoutFromSocialProvider
- loginWithSocialProvider -> loginWithSocialProviderToken
  • Loading branch information
adar2378 committed Sep 22, 2023
1 parent 356ba92 commit 804a5e0
Showing 1 changed file with 18 additions and 7 deletions.
@@ -1,32 +1,33 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:djangoflow_auth/src/blocs/auth_cubit/auth_cubit_base.dart';
import 'package:djangoflow_auth/src/exceptions/login_provider_not_found_exception.dart';
import 'package:djangoflow_auth/src/interfaces/social_login.dart';
import 'package:djangoflow_auth/src/models/social_login_type/social_login_type.dart';
import 'package:djangoflow_openapi/djangoflow_openapi.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';

import 'auth_state.dart';

export 'auth_state.dart';

class AuthCubit extends HydratedCubit<AuthState> {
class AuthCubit extends HydratedAuthCubitBase {
AuthApi? authApi;

static AuthCubit get instance => _instance;
static final AuthCubit _instance = AuthCubit._internal();
AuthCubit._internal() : super(const AuthState.initial());
List<SocialLogin> socialLogins = [];

@override
AuthState? fromJson(Map<String, dynamic> json) => AuthState.fromJson(json);

@override
Map<String, dynamic>? toJson(AuthState state) => state.toJson();

/// Authenticate user with social provider
Future<R?> authenticateWithSocialProvider<R>(SocialLoginType type) async {
/// Authenticate and request token for user from the Social Auth Provider
/// eg, facebook, google etc.
@override
Future<R?> requestTokenFromSocialProvider<R>(SocialLoginType type) async {
final provider = _firstWhereSocialLogin(type);
if (provider == null) {
throw _loginProviderNotFoundException(type);
Expand All @@ -36,7 +37,8 @@ class AuthCubit extends HydratedCubit<AuthState> {
return response;
}

Future<void> logoutWithSocialProvider(SocialLoginType type) async {
@override
Future<void> logoutFromSocialProvider(SocialLoginType type) async {
final socialLogin = _firstWhereSocialLogin(type);
if (socialLogin == null) {
throw _loginProviderNotFoundException(type);
Expand All @@ -58,6 +60,7 @@ class AuthCubit extends HydratedCubit<AuthState> {
);

/// Logout user, also removes token from storage and logs out user from social logins
@override
Future<void> logout() async {
for (final provider in socialLogins) {
await provider.logout();
Expand All @@ -68,6 +71,7 @@ class AuthCubit extends HydratedCubit<AuthState> {
}

/// Register or invite user to the system
@override
Future<UserIdentity?> registerOrInviteUser({
required UserIdentityRequest userIdentityRequest,
}) async =>
Expand All @@ -81,6 +85,7 @@ class AuthCubit extends HydratedCubit<AuthState> {
});

/// Request OTP for verification.
@override
Future<void> requestOTP({required OTPObtainRequest otpObtainRequest}) async =>
_authApiChecker(
() async => (
Expand All @@ -92,6 +97,7 @@ class AuthCubit extends HydratedCubit<AuthState> {

/// Authenticates the user based on provided credentials (e.g., username+password or email+OTP etc)
/// and logs them in by obtaining and processing a JWT token.
@override
Future<void> obtainTokenAndLogin({
required TokenObtainRequest tokenObtainRequest,
}) async =>
Expand All @@ -115,6 +121,7 @@ class AuthCubit extends HydratedCubit<AuthState> {

/// Login user with magic link. magiclink should not be empty
/// It supports only email at the moment.
@override
Future<void> loginWithMagicLink({required String magiclink}) async {
try {
final credentials = utf8
Expand All @@ -132,7 +139,10 @@ class AuthCubit extends HydratedCubit<AuthState> {
}
}

Future<void> loginWithSocialProvider(
/// Login user with social provider token data that was retrieved via [requestTokenFromSocialProvider]
/// This will retrieve token from backend and login user
@override
Future<void> loginWithSocialProviderToken(
{required SocialTokenObtainRequest socialTokenObtainRequest}) async =>
_authApiChecker(() async {
final result = (await authApi?.authSocialCreate(
Expand All @@ -145,6 +155,7 @@ class AuthCubit extends HydratedCubit<AuthState> {
}
});

@override
Future<void> changePassword({
required String id,
required ChangePasswordRequest changePasswordRequest,
Expand Down

0 comments on commit 804a5e0

Please sign in to comment.