workouttest_util/lib/service/firebase_api.dart
2023-02-25 22:19:03 +01:00

422 lines
14 KiB
Dart

import 'dart:math' as math;
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';
import 'package:workouttest_util/model/cache.dart';
import 'package:workouttest_util/util/logging.dart' as logger;
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';
import 'package:workouttest_util/util/track.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<void> initializeFlutterFire() async {
try {
if (kIsWeb) {
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: "AIzaSyBLn7Bz73Z1hB-OhqphBDsskOyGmpI7J8E",
authDomain: "diet4you-cb540.firebaseapp.com",
projectId: "diet4you-cb540",
storageBucket: "diet4you-cb540.appspot.com",
messagingSenderId: "534189506381",
appId: "1:534189506381:web:e885436c9be71ab4998104",
measurementId: "G-9YY4B98J99"),
);
} else {
await Firebase.initializeApp();
}
} on Exception catch (e) {
Track().trackError(e);
log("Error in Firebase init: $e");
}
appleSignInAvailable = await SignInWithApple.isAvailable();
if (kIsWeb) {
/* final fcmToken = await FirebaseMessaging.instance.getToken(
vapidKey:
"BKqkxyTqlGGI6m4gFLa-fFu9kYflyCbLkDKRKihWLqhDyR8oy1ymGzbk9lGcSDW1fd7XZiN2XYA2sDF8yjHdFPg");
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
// Note: This callback is fired at each app startup and whenever a new
// token is generated.
log('FCM token generated');
}).onError((err) {
Track().trackError(err);
log("Error initializing Firebase Messaging for Web $err");
}); */
} else {
try {
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification
badge: true,
sound: true,
);
firebaseRegToken = await FirebaseMessaging.instance.getToken();
Cache().firebaseMessageToken = firebaseRegToken;
log("FirebaseMessaging token $firebaseRegToken");
FirebaseMessaging.onBackgroundMessage(
_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
log('Got a message whilst in the foreground!');
log('Message data: ${message.data}');
if (message.notification != null) {
log('Message also contained a notification: ${message.notification}');
}
});
} catch (e) {
Track().trackError(e);
log("Error initializing Firebase Messaging $e");
}
}
}
Future<String> 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) {
Track().trackError(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<String> 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) {
Track().trackError(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());
Track().trackError(e);
throw Exception(e.toString());
}
return rc;
}
String generateNonce([int length = 32]) {
const 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<Map<String, dynamic>> signInWithApple() async {
Map<String, dynamic> userData = {};
// 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) {
Track().trackError(e);
throw Exception(e);
}
Cache().firebaseUid = userCredential.user!.uid;
log("userCredential: $userCredential");
log("Apple Credentials: ${appleCredential.userIdentifier} state ${appleCredential.state} email ${userCredential.user!.email!}");
userData['email'] = userCredential.user!.email;
return userData;
}
Future<Map<String, dynamic>> registerWithApple() async {
Map<String, dynamic> userData = {};
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) {
Track().trackError(e);
throw Exception(e);
}
Cache().firebaseUid = userCredential.user!.uid;
userData['email'] = userCredential.user!.email;
return userData;
}
Future<Map<String, dynamic>> signInWithGoogle() async {
Map<String, dynamic> userData = {};
// 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) {
Track().trackError(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");
userData['email'] = googleUser.email;
userData['id'] = googleUser.id;
userData['name'] = googleUser.displayName;
return userData;
}
Future<Map<String, dynamic>> registerWithGoogle() async {
Map<String, dynamic> userData = {};
// 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) {
String e = "Google Sign In failed";
Track().trackError(e);
throw Exception(e);
}
// 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 GoogleUser: $googleUser");
Cache().firebaseUid = userCredential.user!.uid;
userData['email'] = googleUser.email;
return userData;
}
Future<Map<String, dynamic>> signInWithFacebook() async {
Map<String, dynamic> 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 {
Track().trackError(Exception(result.message));
throw Exception("Facebook login was not successful");
}
return userData;
}
Future<Map<String, dynamic>> registerWithFacebook() async {
Map<String, dynamic> 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");
// 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");
Cache().firebaseUid = userCredential.user!.uid;
} else {
Track().trackError(Exception(result.message));
throw Exception("Facebook login was not successful");
}
return userData;
}
Future<void> logOutFacebook() async {
await FacebookAuth.instance.logOut();
Cache().accessTokenFacebook = null;
}
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}
Future<void> resetPassword(String email) async {
await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
}
Future<void> 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) {
log('Unable to fetch remote config. Cached or default values will be used: $e');
if (remoteConfig != null) {
await remoteConfig.setDefaults(<String, dynamic>{
'sales_page_text_a': '',
'product_set_2': '',
'registration_skip_color': '',
'email_checkbox': '',
});
Cache().setRemoteConfig(remoteConfig);
}
}
}
}
Future<void> _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}');
}