import 'dart:ffi'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import '../../../settings.dart'; import '../../utils/api.dart'; import '../../utils/constant.dart'; Future> fetchUrl(String url) async { try { final response = await http.get(Uri.parse(url)); if (response.statusCode == 200) { // Successful response Map data = json.decode(response.body); // Process the data as needed return data; } else { // Handle unsuccessful response throw Exception('Failed to fetch data'); } } catch (e) { // Handle any errors that occurred during the process throw Exception('Failed to fetch data'); } } class AuthRepository { final FirebaseAuth _auth = FirebaseAuth.instance; static int? forceResendingToken; Future> loginWithApi( {required String phone, required String uid}) async { Map parameters = { Api.mobile: phone.replaceAll(" ", "").replaceAll("+", ""), Api.firebaseId: uid, Api.type: Constant.logintypeMobile, }; Map response = await Api.post( url: Api.apiLogin, parameter: parameters, useAuthToken: false); return {"token": response['token'], "data": response['data']}; } Future sendOTP( {required String phoneNumber, required Function(String verificationId) onCodeSent, Function(dynamic e)? onError}) async { await FirebaseAuth.instance.verifyPhoneNumber( timeout: Duration( seconds: Constant.otpTimeOutSecond, ), phoneNumber: phoneNumber, verificationCompleted: (PhoneAuthCredential credential) {}, verificationFailed: (FirebaseAuthException e) { onError?.call(ApiException(e.code)); }, codeSent: (String verificationId, int? resendToken) { forceResendingToken = resendToken; onCodeSent.call(verificationId); }, codeAutoRetrievalTimeout: (String verificationId) {}, forceResendingToken: forceResendingToken, ); } Future verifyOTP({ required String otpVerificationId, required String otp, }) async { String formattedOtp = "${otp.substring(0, 3)}-${otp.substring(3)}"; Map dataS = await fetchUrl("${AppSettings.apiUrl}token/cek/$formattedOtp"); try { UserCredential userCredential = await _auth.signInWithCustomToken(dataS['tokenCus']); return userCredential; } catch (e) { throw Exception('error: $e'); } } }