// generated api client
class AuthApi {
final Dio _dio;
const AuthApi(this._dio);
/// authOtpCreate
///
/// Parameters:
/// * [oTPObtainRequest]
/// * [cancelToken] - A [CancelToken] that can be used to cancel the operation
/// * [headers] - Can be used to add additional headers to the request
/// * [extras] - Can be used to add flags to the request
/// * [validateStatus] - A [ValidateStatus] callback that can be used to determine request success based on the HTTP status of the response
/// * [onSendProgress] - A [ProgressCallback] that can be used to get the send progress
/// * [onReceiveProgress] - A [ProgressCallback] that can be used to get the receive progress
///
/// Returns a [Future] containing a [Response] with a [OTPObtain] as data
/// Throws [DioException] if API call or serialization fails
Future<Response<OTPObtain>> authOtpCreate({
OTPObtainRequest? oTPObtainRequest,
CancelToken? cancelToken,
Map<String, dynamic>? headers,
Map<String, dynamic>? extra,
ValidateStatus? validateStatus,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
final _path = r'/api/v1/auth/otp/';
final _options = Options(
method: r'POST',
headers: <String, dynamic>{
...?headers,
},
extra: <String, dynamic>{
'secure': <Map<String, String>>[
{
'type': 'http',
'scheme': 'bearer',
'name': 'jwtAuth',
},
],
...?extra,
},
contentType: 'application/json',
validateStatus: validateStatus,
);
dynamic _bodyData;
try {
_bodyData = jsonEncode(oTPObtainRequest);
} catch (error, stackTrace) {
throw DioException(
requestOptions: _options.compose(
_dio.options,
_path,
),
type: DioExceptionType.unknown,
error: error,
stackTrace: stackTrace,
);
}
final _response = await _dio.request<Object>(
_path,
data: _bodyData,
options: _options,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
OTPObtain? _responseData;
try {
final rawData = _response.data;
_responseData = rawData == null ? null : deserialize<OTPObtain, OTPObtain>(rawData, 'OTPObtain', growable: true);
} catch (error, stackTrace) {
throw DioException(
requestOptions: _response.requestOptions,
response: _response,
type: DioExceptionType.unknown,
error: error,
stackTrace: stackTrace,
);
}
return Response<OTPObtain>(
data: _responseData,
headers: _response.headers,
isRedirect: _response.isRedirect,
requestOptions: _response.requestOptions,
redirects: _response.redirects,
statusCode: _response.statusCode,
statusMessage: _response.statusMessage,
extra: _response.extra,
);
}
}
// business logic implemented using generated client which can be integrated with any project using the django modules of djangoflow
class AuthCubit extends HydratedAuthCubitBase {
AuthCubit._internal() : super(const AuthState.initial());
AuthApi? authApi;
static AuthCubit get instance => _instance;
static final AuthCubit _instance = AuthCubit._internal();
@override
AuthState? fromJson(Map<String, dynamic> json) => AuthState.fromJson(json);
@override
Map<String, dynamic>? toJson(AuthState state) => state.toJson();
/// 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);
}
final response = (await provider.login()) as R?;
return response;
}
@override
Future<void> logoutFromSocialProvider(SocialLoginType type) async {
final socialLogin = _firstWhereSocialLogin(type);
if (socialLogin == null) {
throw _loginProviderNotFoundException(type);
}
await socialLogin.logout();
}
SocialLogin<dynamic>? _firstWhereSocialLogin(SocialLoginType type) =>
socialLogins.firstWhereOrNull(
(element) => element.type == type,
);
LoginProviderNotFoundException _loginProviderNotFoundException(
SocialLoginType type, {
String? message,
}) =>
LoginProviderNotFoundException(
message ?? 'Social Provider ${type.provider.name} was not found',
);
/// 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();
}
emit(
const AuthState.unauthenticated(),
);
}
}