import 'dart:math' as math; import 'dart:convert'; import 'package:crypto/crypto.dart'; import 'package:aitrainer_app/model/cache.dart'; import 'package:aitrainer_app/service/logging.dart' as logger; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:sign_in_with_apple/sign_in_with_apple.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter_facebook_auth/flutter_facebook_auth.dart'; import 'package:google_sign_in/google_sign_in.dart'; class FirebaseApi with logger.Logging { bool appleSignInAvailable = false; static final FirebaseAuth auth = FirebaseAuth.instance; static const String SIGN_IN_OK = "OK"; static const String SIGN_IN_NOT_FOUND = "user-not-found"; static const String SIGN_IN_WRONG_PWD = "wrong-password"; static const String REGISTER_WEAK_PWD = "weak-password"; static const String REGISTER_EMAIL_IN_USE = "email-already-in-use"; late UserCredential userCredential; String? firebaseRegToken; factory FirebaseApi() => FirebaseApi._internal(); FirebaseApi._internal(); // Define an async function to initialize FlutterFire Future initializeFlutterFire() async { try { // Wait for Firebase to initialize and set `_initialized` state to true await Firebase.initializeApp(); this.appleSignInAvailable = await SignInWithApple.isAvailable(); await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, // Required to display a heads up notification badge: true, sound: true, ); this.firebaseRegToken = await FirebaseMessaging.instance.getToken(); Cache().firebaseMessageToken = firebaseRegToken; log("FirebaseMessaging token $firebaseRegToken"); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); } }); } catch (e) { // Set `_error` state to true if Firebase initialization fails Sentry.captureException(e); log("Error initializing Firebase"); } } Future signInEmail(String? email, String? password) async { if (email == null) { throw Exception("Please type an email address"); } if (password == null) { throw Exception("Password too short"); } String rc = SIGN_IN_OK; try { userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password); Cache().firebaseUid = userCredential.user!.uid; } on FirebaseAuthException catch (e) { Sentry.captureException(e); if (e.code == 'user-not-found') { log('No user found for that email.'); rc = SIGN_IN_NOT_FOUND; } else if (e.code == 'wrong-password') { log('Wrong password provided for that user.'); rc = SIGN_IN_WRONG_PWD; throw Exception("Customer does not exist or the password is wrong"); } return e.code; } return rc; } Future registerEmail(String email, String password) async { String rc = SIGN_IN_OK; try { userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password); Cache().firebaseUid = userCredential.user!.uid; } on FirebaseAuthException catch (e) { Sentry.captureException(e); if (e.code == 'weak-password') { log('The password provided is too weak.'); rc = REGISTER_WEAK_PWD; throw Exception("Password too short"); } else if (e.code == 'email-already-in-use') { log('The account already exists for that email.'); rc = REGISTER_EMAIL_IN_USE; throw Exception("The email address has been registered already"); } } catch (e) { log(e.toString()); Sentry.captureException(e); throw Exception(e.toString()); } return rc; } String generateNonce([int length = 32]) { final charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._'; final random = math.Random.secure(); return List.generate(length, (_) => charset[random.nextInt(charset.length)]).join(); } /// Returns the sha256 hash of [input] in hex notation. String sha256ofString(String input) { final bytes = utf8.encode(input); final digest = sha256.convert(bytes); return digest.toString(); } Future> signInWithApple() async { Map userData = Map(); // To prevent replay attacks with the credential returned from Apple, we // include a nonce in the credential request. When signing in with // Firebase, the nonce in the id token returned by Apple, is expected to // match the sha256 hash of `rawNonce`. final rawNonce = generateNonce(); final nonce = sha256ofString(rawNonce); // Request credential for the currently signed in Apple account. final appleCredential = await SignInWithApple.getAppleIDCredential( scopes: [ AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName, ], nonce: nonce, ); // Create an `OAuthCredential` from the credential returned by Apple. final oauthCredential = OAuthProvider("apple.com").credential( idToken: appleCredential.identityToken, rawNonce: rawNonce, ); UserCredential? userCredential; try { // Sign in the user with Firebase. If the nonce we generated earlier does // not match the nonce in `appleCredential.identityToken`, sign in will fail. userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); } on FirebaseAuthException catch(e) { Sentry.captureException(e); throw Exception(e); } Cache().firebaseUid = userCredential.user!.uid; log("userCredential: " + userCredential.toString()); log("Apple Credentials: ${appleCredential.userIdentifier} state ${appleCredential.state} email ${userCredential.user!.email!}"); userData['email'] = userCredential.user!.email; return userData; } Future> registerWithApple() async { Map userData = Map(); final rawNonce = generateNonce(); final nonce = sha256ofString(rawNonce); // Request credential for the currently signed in Apple account. final appleCredential = await SignInWithApple.getAppleIDCredential( scopes: [ AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName, ], nonce: nonce, ); final oauthCredential = OAuthProvider("apple.com").credential( idToken: appleCredential.identityToken, rawNonce: rawNonce, ); UserCredential? userCredential; try { // Sign in the user with Firebase. If the nonce we generated earlier does // not match the nonce in `appleCredential.identityToken`, sign in will fail. userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); } on FirebaseAuthException catch(e) { Sentry.captureException(e); throw Exception(e); } Cache().firebaseUid = userCredential.user!.uid; userData['email'] = userCredential.user!.email; return userData; } Future> signInWithGoogle() async { Map userData = Map(); // Trigger the authentication flow GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ 'email', 'https://www.googleapis.com/auth/contacts.readonly', ], ); final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); if (googleUser == null) { Sentry.captureException(new Exception("Google Sign In failed")); throw Exception("Google Sign In failed"); } // Obtain the auth details from the request final GoogleSignInAuthentication googleAuth = await googleUser.authentication; // Create a new credential final OAuthCredential credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential); Cache().firebaseUid = userCredential.user!.uid; log("GoogleUser: " + googleUser.toString()); userData['email'] = googleUser.email; userData['id'] = googleUser.id; userData['name'] = googleUser.displayName; return userData; } Future> registerWithGoogle() async { Map userData = Map(); // Trigger the authentication flow GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ 'email', 'https://www.googleapis.com/auth/contacts.readonly', ], ); final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); if (googleUser == null) { Sentry.captureException(new Exception("Google Sign In failed")); throw Exception("Google Sign In failed"); } // Obtain the auth details from the request final GoogleSignInAuthentication googleAuth = await googleUser.authentication; // Create a new credential final OAuthCredential credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); final userCredential = await FirebaseAuth.instance.signInWithCredential(credential); log("Google credentials: " + credential.toString() + " GoogleUser: " + googleUser.toString()); Cache().firebaseUid = userCredential.user!.uid; userData['email'] = googleUser.email; return userData; } Future> signInWithFacebook() async { Map userData; // by default the login method has the next permissions ['email','public_profile'] final LoginResult result = await FacebookAuth.instance.login(); if (result.status == LoginStatus.success) { final AccessToken accessToken = result.accessToken!; log(accessToken.toJson().toString()); Cache().accessTokenFacebook = accessToken; // get the user data userData = await FacebookAuth.instance.getUserData(); Cache().firebaseUid = userData['id']; log(userData.toString()); } else { Sentry.captureException(new Exception(result.message)); throw Exception("Facebook login was not successful"); } return userData; } Future> registerWithFacebook() async { Map userData; // by default the login method has the next permissions ['email','public_profile'] final LoginResult result = await FacebookAuth.instance.login(); if (result.status == LoginStatus.success) { final AccessToken accessToken = result.accessToken!; Cache().accessTokenFacebook = accessToken; // get the user data userData = await FacebookAuth.instance.getUserData(); log("FB user data: " + userData.toString()); // Create a credential from the access token final OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(accessToken.token); // Once signed in, return the UserCredential final userCredential = await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential); log("Email by FB: " + userData['email'] + " FB credential: " + userCredential.toString()); Cache().firebaseUid = userCredential.user!.uid; } else { Sentry.captureException(new Exception(result.message)); throw Exception("Facebook login was not successful"); } return userData; } Future logOutFacebook() async { await FacebookAuth.instance.logOut(); Cache().accessTokenFacebook = null; } Future signOut() async { await FirebaseAuth.instance.signOut(); } Future resetPassword(String email) async { await FirebaseAuth.instance.sendPasswordResetEmail(email: email); } Future setupRemoteConfig() async { //initializeFlutterFire(); FirebaseRemoteConfig? remoteConfig; try { remoteConfig = FirebaseRemoteConfig.instance; await remoteConfig.setConfigSettings(RemoteConfigSettings( fetchTimeout: const Duration(seconds: 10), minimumFetchInterval: const Duration(seconds: 1), )); //RemoteConfigValue(null, ValueSource.valueStatic); //Cache().setRemoteConfig(remoteConfig); } on Exception catch (e) { print('Unable to fetch remote config. Cached or default values will be used: $e'); if (remoteConfig != null) { await remoteConfig.setDefaults({ 'sales_page_text_a': '', 'product_set_2': '', 'registration_skip_color': '', 'email_checkbox': '', }); Cache().setRemoteConfig(remoteConfig); } } } } Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, // make sure you call `initializeApp` before using other Firebase services. print('Handling a background message ${message.messageId}'); } /* Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { print('Handling a background message: ${message.messageId}'); if (!StringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) || !StringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true)) { print('message also contained a notification: ${message.notification}'); String? imageUrl; imageUrl ??= message.notification!.android?.imageUrl; imageUrl ??= message.notification!.apple?.imageUrl; Map notificationAdapter = { NOTIFICATION_CHANNEL_KEY: 'basic_channel', NOTIFICATION_ID: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_ID] ?? message.messageId ?? math.Random().nextInt(2147483647), NOTIFICATION_TITLE: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_TITLE] ?? message.notification?.title, NOTIFICATION_BODY: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_BODY] ?? message.notification?.body, NOTIFICATION_LAYOUT: StringUtils.isNullOrEmpty(imageUrl) ? 'Default' : 'BigPicture', NOTIFICATION_BIG_PICTURE: imageUrl }; AwesomeNotifications().createNotificationFromJsonData(notificationAdapter); } else { AwesomeNotifications().createNotificationFromJsonData(message.data); } } */