v.1.0.11 no FCM with web

This commit is contained in:
bossanyit 2023-02-25 22:19:03 +01:00
parent 6ac35b67ab
commit a4a96cce97
3 changed files with 84 additions and 72 deletions

View File

@ -1,5 +1,8 @@
Workout Test and Diet 4 You Common Util Functions Workout Test and Diet 4 You Common Util Functions
Version 1.0.11
No FCM on Web
Version 1.0.11 Version 1.0.11
Sentry and logging only in debugMode Sentry and logging only in debugMode

View File

@ -34,7 +34,6 @@ class FirebaseApi with logger.Logging {
// Define an async function to initialize FlutterFire // Define an async function to initialize FlutterFire
Future<void> initializeFlutterFire() async { Future<void> initializeFlutterFire() async {
try { try {
if (kIsWeb) { if (kIsWeb) {
await Firebase.initializeApp( await Firebase.initializeApp(
options: const FirebaseOptions( options: const FirebaseOptions(
@ -44,13 +43,12 @@ class FirebaseApi with logger.Logging {
storageBucket: "diet4you-cb540.appspot.com", storageBucket: "diet4you-cb540.appspot.com",
messagingSenderId: "534189506381", messagingSenderId: "534189506381",
appId: "1:534189506381:web:e885436c9be71ab4998104", appId: "1:534189506381:web:e885436c9be71ab4998104",
measurementId: "G-9YY4B98J99" measurementId: "G-9YY4B98J99"),
),
); );
} else { } else {
await Firebase.initializeApp(); await Firebase.initializeApp();
} }
} on Exception catch(e) { } on Exception catch (e) {
Track().trackError(e); Track().trackError(e);
log("Error in Firebase init: $e"); log("Error in Firebase init: $e");
} }
@ -58,23 +56,21 @@ class FirebaseApi with logger.Logging {
appleSignInAvailable = await SignInWithApple.isAvailable(); appleSignInAvailable = await SignInWithApple.isAvailable();
if (kIsWeb) { if (kIsWeb) {
final fcmToken = await FirebaseMessaging.instance.getToken(vapidKey: "BKqkxyTqlGGI6m4gFLa-fFu9kYflyCbLkDKRKihWLqhDyR8oy1ymGzbk9lGcSDW1fd7XZiN2XYA2sDF8yjHdFPg"); /* final fcmToken = await FirebaseMessaging.instance.getToken(
FirebaseMessaging.instance.onTokenRefresh vapidKey:
.listen((fcmToken) { "BKqkxyTqlGGI6m4gFLa-fFu9kYflyCbLkDKRKihWLqhDyR8oy1ymGzbk9lGcSDW1fd7XZiN2XYA2sDF8yjHdFPg");
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
// Note: This callback is fired at each app startup and whenever a new // Note: This callback is fired at each app startup and whenever a new
// token is generated. // token is generated.
log('FCM token generated'); log('FCM token generated');
}).onError((err) {
})
.onError((err) {
Track().trackError(err); Track().trackError(err);
log("Error initializing Firebase Messaging for Web $err"); log("Error initializing Firebase Messaging for Web $err");
}); }); */
} else { } else {
try { try {
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification alert: true, // Required to display a heads up notification
badge: true, badge: true,
sound: true, sound: true,
@ -83,7 +79,8 @@ class FirebaseApi with logger.Logging {
Cache().firebaseMessageToken = firebaseRegToken; Cache().firebaseMessageToken = firebaseRegToken;
log("FirebaseMessaging token $firebaseRegToken"); log("FirebaseMessaging token $firebaseRegToken");
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); FirebaseMessaging.onBackgroundMessage(
_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) { FirebaseMessaging.onMessage.listen((RemoteMessage message) {
log('Got a message whilst in the foreground!'); log('Got a message whilst in the foreground!');
@ -109,7 +106,8 @@ class FirebaseApi with logger.Logging {
} }
String rc = SIGN_IN_OK; String rc = SIGN_IN_OK;
try { try {
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password); userCredential = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
Cache().firebaseUid = userCredential.user!.uid; Cache().firebaseUid = userCredential.user!.uid;
} on FirebaseAuthException catch (e) { } on FirebaseAuthException catch (e) {
Track().trackError(e); Track().trackError(e);
@ -129,7 +127,8 @@ class FirebaseApi with logger.Logging {
Future<String> registerEmail(String email, String password) async { Future<String> registerEmail(String email, String password) async {
String rc = SIGN_IN_OK; String rc = SIGN_IN_OK;
try { try {
userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password); userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
Cache().firebaseUid = userCredential.user!.uid; Cache().firebaseUid = userCredential.user!.uid;
} on FirebaseAuthException catch (e) { } on FirebaseAuthException catch (e) {
Track().trackError(e); Track().trackError(e);
@ -151,9 +150,11 @@ class FirebaseApi with logger.Logging {
} }
String generateNonce([int length = 32]) { String generateNonce([int length = 32]) {
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._'; const charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
final random = math.Random.secure(); final random = math.Random.secure();
return List.generate(length, (_) => charset[random.nextInt(charset.length)]).join(); return List.generate(length, (_) => charset[random.nextInt(charset.length)])
.join();
} }
/// Returns the sha256 hash of [input] in hex notation. /// Returns the sha256 hash of [input] in hex notation.
@ -191,8 +192,9 @@ class FirebaseApi with logger.Logging {
try { try {
// Sign in the user with Firebase. If the nonce we generated earlier does // Sign in the user with Firebase. If the nonce we generated earlier does
// not match the nonce in `appleCredential.identityToken`, sign in will fail. // not match the nonce in `appleCredential.identityToken`, sign in will fail.
userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); userCredential =
} on FirebaseAuthException catch(e) { await FirebaseAuth.instance.signInWithCredential(oauthCredential);
} on FirebaseAuthException catch (e) {
Track().trackError(e); Track().trackError(e);
throw Exception(e); throw Exception(e);
} }
@ -229,8 +231,9 @@ class FirebaseApi with logger.Logging {
try { try {
// Sign in the user with Firebase. If the nonce we generated earlier does // Sign in the user with Firebase. If the nonce we generated earlier does
// not match the nonce in `appleCredential.identityToken`, sign in will fail. // not match the nonce in `appleCredential.identityToken`, sign in will fail.
userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); userCredential =
} on FirebaseAuthException catch(e) { await FirebaseAuth.instance.signInWithCredential(oauthCredential);
} on FirebaseAuthException catch (e) {
Track().trackError(e); Track().trackError(e);
throw Exception(e); throw Exception(e);
} }
@ -260,7 +263,8 @@ class FirebaseApi with logger.Logging {
} }
// Obtain the auth details from the request // Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser.authentication; final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
// Create a new credential // Create a new credential
final OAuthCredential credential = GoogleAuthProvider.credential( final OAuthCredential credential = GoogleAuthProvider.credential(
@ -268,7 +272,8 @@ class FirebaseApi with logger.Logging {
idToken: googleAuth.idToken, idToken: googleAuth.idToken,
); );
UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential); UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
Cache().firebaseUid = userCredential.user!.uid; Cache().firebaseUid = userCredential.user!.uid;
log("GoogleUser: $googleUser"); log("GoogleUser: $googleUser");
@ -298,7 +303,8 @@ class FirebaseApi with logger.Logging {
} }
// Obtain the auth details from the request // Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser.authentication; final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
// Create a new credential // Create a new credential
final OAuthCredential credential = GoogleAuthProvider.credential( final OAuthCredential credential = GoogleAuthProvider.credential(
@ -306,7 +312,8 @@ class FirebaseApi with logger.Logging {
idToken: googleAuth.idToken, idToken: googleAuth.idToken,
); );
final userCredential = await FirebaseAuth.instance.signInWithCredential(credential); final userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
log("Google credentials: $credential GoogleUser: $googleUser"); log("Google credentials: $credential GoogleUser: $googleUser");
Cache().firebaseUid = userCredential.user!.uid; Cache().firebaseUid = userCredential.user!.uid;
@ -350,10 +357,12 @@ class FirebaseApi with logger.Logging {
log("FB user data: $userData"); log("FB user data: $userData");
// Create a credential from the access token // Create a credential from the access token
final OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(accessToken.token); final OAuthCredential facebookAuthCredential =
FacebookAuthProvider.credential(accessToken.token);
// Once signed in, return the UserCredential // Once signed in, return the UserCredential
final userCredential = await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential); final userCredential = await FirebaseAuth.instance
.signInWithCredential(facebookAuthCredential);
log("Email by FB: ${userData['email']} FB credential: $userCredential"); log("Email by FB: ${userData['email']} FB credential: $userCredential");
Cache().firebaseUid = userCredential.user!.uid; Cache().firebaseUid = userCredential.user!.uid;

View File

@ -1,6 +1,6 @@
name: workouttest_util name: workouttest_util
description: Workout Test app and web functions. description: Workout Test app and web functions.
version: 1.0.10 version: 1.0.11
homepage: homepage:
environment: environment: