83 lines
2.6 KiB
Dart
83 lines
2.6 KiB
Dart
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<Map<String, dynamic>> fetchUrl(String url) async {
|
|
try {
|
|
final response = await http.get(Uri.parse(url));
|
|
if (response.statusCode == 200) {
|
|
// Successful response
|
|
Map<String, dynamic> 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<Map<String, dynamic>> loginWithApi(
|
|
{required String phone, required String uid}) async {
|
|
Map<String, String> parameters = {
|
|
Api.mobile: phone.replaceAll(" ", "").replaceAll("+", ""),
|
|
Api.firebaseId: uid,
|
|
Api.type: Constant.logintypeMobile,
|
|
};
|
|
|
|
Map<String, dynamic> response = await Api.post(
|
|
url: Api.apiLogin, parameter: parameters, useAuthToken: false);
|
|
|
|
return {"token": response['token'], "data": response['data']};
|
|
}
|
|
|
|
Future<void> 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<UserCredential> verifyOTP({
|
|
required String otpVerificationId,
|
|
required String otp,
|
|
}) async {
|
|
String formattedOtp = "${otp.substring(0, 3)}-${otp.substring(3)}";
|
|
Map<String, dynamic> dataS =
|
|
await fetchUrl("${AppSettings.apiUrl}token/cek/$formattedOtp");
|
|
try {
|
|
UserCredential userCredential = await _auth.signInWithCustomToken(dataS['tokenCus']);
|
|
return userCredential;
|
|
} catch (e) {
|
|
throw Exception('error: $e');
|
|
}
|
|
}
|
|
}
|