diff --git a/analytics-next/package.json b/analytics-next/package.json index 0fb07418..ee4e36ff 100644 --- a/analytics-next/package.json +++ b/analytics-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/appcheck-next/package.json b/appcheck-next/package.json index ad78db9f..e863723e 100644 --- a/appcheck-next/package.json +++ b/appcheck-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/auth-next/apple.js b/auth-next/apple.js index f9670b4c..2777a085 100644 --- a/auth-next/apple.js +++ b/auth-next/apple.js @@ -38,6 +38,7 @@ function appleSignInPopup(provider) { const accessToken = credential.accessToken; const idToken = credential.idToken; + // IdP data available using getAdditionalUserInfo(result) // ... }) .catch((error) => { diff --git a/auth-next/custom-email-handler.js b/auth-next/custom-email-handler.js index 728baff8..5fcc1f71 100644 --- a/auth-next/custom-email-handler.js +++ b/auth-next/custom-email-handler.js @@ -28,8 +28,8 @@ function handleUserManagementQueryParams() { // Configure the Firebase SDK. // This is the minimum configuration required for the API to be used. const config = { - 'apiKey': "YOU_API_KEY" // Copy this key from the web initialization - // snippet found in the Firebase console. + 'apiKey': "YOUR_API_KEY" // Copy this key from the web initialization + // snippet found in the Firebase console. }; const app = initializeApp(config); const auth = getAuth(app); diff --git a/auth-next/email-link-auth.js b/auth-next/email-link-auth.js index 59ea2115..fde67663 100644 --- a/auth-next/email-link-auth.js +++ b/auth-next/email-link-auth.js @@ -19,7 +19,8 @@ function emailLinkActionCodeSettings() { installApp: true, minimumVersion: '12' }, - dynamicLinkDomain: 'example.page.link' + // The domain must be configured in Firebase Hosting and owned by the project. + linkDomain: 'custom-domain.com' }; // [END auth_email_link_actioncode_settings] } @@ -68,11 +69,13 @@ function emailLinkComplete() { .then((result) => { // Clear email from storage. window.localStorage.removeItem('emailForSignIn'); - // You can access the new user via result.user - // Additional user info profile not available via: - // result.additionalUserInfo.profile == null + // You can access the new user by importing getAdditionalUserInfo + // and calling it with result: + // getAdditionalUserInfo(result) + // You can access the user's profile via: + // getAdditionalUserInfo(result)?.profile // You can check if the user is new or existing: - // result.additionalUserInfo.isNewUser + // getAdditionalUserInfo(result)?.isNewUser }) .catch((error) => { // Some error occurred, you can inspect the code: error.code diff --git a/auth-next/email.js b/auth-next/email.js index b00551c8..26391f81 100644 --- a/auth-next/email.js +++ b/auth-next/email.js @@ -32,7 +32,7 @@ function signUpWithEmailPassword() { const auth = getAuth(); createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { - // Signed in + // Signed up const user = userCredential.user; // ... }) diff --git a/auth-next/emulator-suite.js b/auth-next/emulator-suite.js index 66507de3..13b1ba95 100644 --- a/auth-next/emulator-suite.js +++ b/auth-next/emulator-suite.js @@ -6,7 +6,7 @@ function emulatorConnect() { const { getAuth, connectAuthEmulator } = require("firebase/auth"); const auth = getAuth(); - connectAuthEmulator(auth, "http://localhost:9099"); + connectAuthEmulator(auth, "http://127.0.0.1:9099"); // [END auth_emulator_connect] } diff --git a/auth-next/facebook.js b/auth-next/facebook.js index 67b397f2..3c6feb8a 100644 --- a/auth-next/facebook.js +++ b/auth-next/facebook.js @@ -33,6 +33,7 @@ function facebookSignInPopup(provider) { const credential = FacebookAuthProvider.credentialFromResult(result); const accessToken = credential.accessToken; + // IdP data available using getAdditionalUserInfo(result) // ... }) .catch((error) => { @@ -61,6 +62,8 @@ function facebookSignInRedirectResult() { const token = credential.accessToken; const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/auth-next/github.js b/auth-next/github.js index babfd1ee..4c128721 100644 --- a/auth-next/github.js +++ b/auth-next/github.js @@ -40,6 +40,7 @@ function githubSignInPopup(provider) { // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. @@ -70,6 +71,8 @@ function githubSignInRedirectResult() { // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/auth-next/google-signin.js b/auth-next/google-signin.js index 136c26b5..025eebd6 100644 --- a/auth-next/google-signin.js +++ b/auth-next/google-signin.js @@ -33,6 +33,7 @@ function googleSignInPopup(provider) { const token = credential.accessToken; // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. @@ -60,6 +61,8 @@ function googleSignInRedirectResult() { // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/auth-next/index.js b/auth-next/index.js index b4a17903..04ac7d03 100644 --- a/auth-next/index.js +++ b/auth-next/index.js @@ -52,7 +52,7 @@ function authStateListener() { onAuthStateChanged(auth, (user) => { if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/v8/firebase.User const uid = user.uid; // ... } else { @@ -72,7 +72,7 @@ function currentUser() { if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/v8/firebase.User // ... } else { // No user is signed in. @@ -87,7 +87,7 @@ function setLanguageCode() { const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. - // firebase.auth().useDeviceLanguage(); + // auth.useDeviceLanguage(); // [END auth_set_language_code] } diff --git a/auth-next/link-multiple-accounts.js b/auth-next/link-multiple-accounts.js index 34c0db58..1938aafc 100644 --- a/auth-next/link-multiple-accounts.js +++ b/auth-next/link-multiple-accounts.js @@ -182,3 +182,61 @@ function unlink(providerId) { }); // [END auth_unlink_provider] } + +function accountExistsPopup(auth, facebookProvider, goToApp, promptUserForPassword, promptUserForSignInMethod, getProviderForProviderId) { + // [START account_exists_popup] + const { signInWithPopup, signInWithEmailAndPassword, linkWithCredential } = require("firebase/auth"); + + // User tries to sign in with Facebook. + signInWithPopup(auth, facebookProvider).catch((error) => { + // User's email already exists. + if (error.code === 'auth/account-exists-with-different-credential') { + // The pending Facebook credential. + const pendingCred = error.credential; + // The provider account's email address. + const email = error.customData.email; + + // Present the user with a list of providers they might have + // used to create the original account. + // Then, ask the user to sign in with the existing provider. + const method = promptUserForSignInMethod(); + + if (method === 'password') { + // TODO: Ask the user for their password. + // In real scenario, you should handle this asynchronously. + const password = promptUserForPassword(); + signInWithEmailAndPassword(auth, email, password).then((result) => { + return linkWithCredential(result.user, pendingCred); + }).then(() => { + // Facebook account successfully linked to the existing user. + goToApp(); + }); + return; + } + + // All other cases are external providers. + // Construct provider object for that provider. + // TODO: Implement getProviderForProviderId. + const provider = getProviderForProviderId(method); + // At this point, you should let the user know that they already have an + // account with a different provider, and validate they want to sign in + // with the new provider. + // Note: Browsers usually block popups triggered asynchronously, so in + // real app, you should ask the user to click on a "Continue" button + // that will trigger signInWithPopup(). + signInWithPopup(auth, provider).then((result) => { + // Note: Identity Platform doesn't control the provider's sign-in + // flow, so it's possible for the user to sign in with an account + // with a different email from the first one. + + // Link the Facebook credential. We have access to the pending + // credential, so we can directly call the link method. + linkWithCredential(result.user, pendingCred).then((userCred) => { + // Success. + goToApp(); + }); + }); + } +}); +// [END account_exists_popup] +} diff --git a/auth-next/package.json b/auth-next/package.json index 48adc595..8d43bad1 100644 --- a/auth-next/package.json +++ b/auth-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/auth-next/phone-auth.js b/auth-next/phone-auth.js index cd894c64..99492f99 100644 --- a/auth-next/phone-auth.js +++ b/auth-next/phone-auth.js @@ -15,13 +15,13 @@ function recaptchaVerifierInvisible() { const { getAuth, RecaptchaVerifier } = require("firebase/auth"); const auth = getAuth(); - window.recaptchaVerifier = new RecaptchaVerifier('sign-in-button', { + window.recaptchaVerifier = new RecaptchaVerifier(auth, 'sign-in-button', { 'size': 'invisible', 'callback': (response) => { // reCAPTCHA solved, allow signInWithPhoneNumber. onSignInSubmit(); } - }, auth); + }); // [END auth_phone_recaptcha_verifier_invisible] } @@ -30,7 +30,7 @@ function recaptchaVerifierVisible() { const { getAuth, RecaptchaVerifier } = require("firebase/auth"); const auth = getAuth(); - window.recaptchaVerifier = new RecaptchaVerifier('recaptcha-container', { + window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', { 'size': 'normal', 'callback': (response) => { // reCAPTCHA solved, allow signInWithPhoneNumber. @@ -40,7 +40,7 @@ function recaptchaVerifierVisible() { // Response expired. Ask user to solve reCAPTCHA again. // ... } - }, auth); + }); // [END auth_phone_recaptcha_verifier_visible] } @@ -49,7 +49,7 @@ function recaptchaVerifierSimple() { const { getAuth, RecaptchaVerifier } = require("firebase/auth"); const auth = getAuth(); - window.recaptchaVerifier = new RecaptchaVerifier('recaptcha-container', {}, auth); + window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', {}); // [END auth_phone_recaptcha_verifier_simple] } diff --git a/auth-next/twitter.js b/auth-next/twitter.js index a386e4ab..f96e574b 100644 --- a/auth-next/twitter.js +++ b/auth-next/twitter.js @@ -30,6 +30,7 @@ function twitterSignInPopup(provider) { // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. @@ -60,6 +61,8 @@ function twitterSignInRedirectResult() { // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/auth/apple.js b/auth/apple.js index e99349c8..00e2598e 100644 --- a/auth/apple.js +++ b/auth/apple.js @@ -40,7 +40,8 @@ function appleSignInPopup(provider) { var accessToken = credential.accessToken; var idToken = credential.idToken; - // ... + // IdP data available using getAdditionalUserInfo(result) + // ... }) .catch((error) => { // Handle Errors here. @@ -77,6 +78,7 @@ function appleSignInRedirectResult() { var accessToken = credential.accessToken; var idToken = credential.idToken; + // IdP data available in result.additionalUserInfo.profile. // ... } // The signed-in user info. @@ -117,7 +119,8 @@ function appleReauthenticatePopup() { var accessToken = credential.accessToken; var idToken = credential.idToken; - // ... + // IdP data available in result.additionalUserInfo.profile. + // ... }) .catch((error) => { // Handle Errors here. diff --git a/auth/emulator-suite.js b/auth/emulator-suite.js index b14a72db..b0f815e2 100644 --- a/auth/emulator-suite.js +++ b/auth/emulator-suite.js @@ -4,7 +4,7 @@ import "firebase/auth"; function emulatorConnect() { // [START auth_emulator_connect] const auth = firebase.auth(); - auth.useEmulator("http://localhost:9099"); + auth.useEmulator("http://127.0.0.1:9099"); // [END auth_emulator_connect] } diff --git a/auth/facebook.js b/auth/facebook.js index 4259960d..75e99d43 100644 --- a/auth/facebook.js +++ b/auth/facebook.js @@ -31,6 +31,8 @@ function facebookSignInPopup(provider) { // The signed-in user info. var user = result.user; + // IdP data available in result.additionalUserInfo.profile. + // ... // This gives you a Facebook Access Token. You can use it to access the Facebook API. var accessToken = credential.accessToken; @@ -66,6 +68,8 @@ function facebookSignInRedirectResult() { } // The signed-in user info. var user = result.user; + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; diff --git a/auth/github.js b/auth/github.js index 6ae1e531..8ae9b39c 100644 --- a/auth/github.js +++ b/auth/github.js @@ -40,7 +40,8 @@ function githubSignInPopup(provider) { // The signed-in user info. var user = result.user; - // ... + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; @@ -70,6 +71,8 @@ function githubSignInRedirectResult() { // The signed-in user info. var user = result.user; + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; diff --git a/auth/google-signin.js b/auth/google-signin.js index fcc32dc8..06a488e4 100644 --- a/auth/google-signin.js +++ b/auth/google-signin.js @@ -34,7 +34,8 @@ function googleSignInPopup(provider) { var token = credential.accessToken; // The signed-in user info. var user = result.user; - // ... + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; @@ -63,6 +64,8 @@ function googleSignInRedirectResult() { } // The signed-in user info. var user = result.user; + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; diff --git a/auth/index.js b/auth/index.js index 6567c030..35546aa3 100644 --- a/auth/index.js +++ b/auth/index.js @@ -43,7 +43,7 @@ function authStateListener() { firebase.auth().onAuthStateChanged((user) => { if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/v8/firebase.User var uid = user.uid; // ... } else { @@ -60,7 +60,7 @@ function currentUser() { if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/v8/firebase.User // ... } else { // No user is signed in. diff --git a/auth/link-multiple-accounts.js b/auth/link-multiple-accounts.js index 30f5a2ec..8eed77b8 100644 --- a/auth/link-multiple-accounts.js +++ b/auth/link-multiple-accounts.js @@ -166,3 +166,59 @@ function unlink(providerId) { }); // [END auth_unlink_provider] } + +function accountExistsPopup(facebookProvider, goToApp, promptUserForPassword, promptUserForSignInMethod, getProviderForProviderId) { + // [START account_exists_popup] + // User tries to sign in with Facebook. + auth.signInWithPopup(facebookProvider).catch((error) => { + // User's email already exists. + if (error.code === 'auth/account-exists-with-different-credential') { + // The pending Facebook credential. + const pendingCred = error.credential; + // The provider account's email address. + const email = error.email; + + // Present the user with a list of providers they might have + // used to create the original account. + // Then, ask the user to sign in with the existing provider. + const method = promptUserForSignInMethod(); + + if (method === 'password') { + // TODO: Ask the user for their password. + // In real scenario, you should handle this asynchronously. + const password = promptUserForPassword(); + auth.signInWithEmailAndPassword(email, password).then((result) => { + return result.user.linkWithCredential(pendingCred); + }).then(() => { + // Facebook account successfully linked to the existing user. + goToApp(); + }); + return; + } + + // All other cases are external providers. + // Construct provider object for that provider. + // TODO: Implement getProviderForProviderId. + const provider = getProviderForProviderId(method); + // At this point, you should let the user know that they already have an + // account with a different provider, and validate they want to sign in + // with the new provider. + // Note: Browsers usually block popups triggered asynchronously, so in + // real app, you should ask the user to click on a "Continue" button + // that will trigger signInWithPopup(). + auth.signInWithPopup(provider).then((result) => { + // Note: Identity Platform doesn't control the provider's sign-in + // flow, so it's possible for the user to sign in with an account + // with a different email from the first one. + + // Link the Facebook credential. We have access to the pending + // credential, so we can directly call the link method. + result.user.linkWithCredential(pendingCred).then((userCred) => { + // Success. + goToApp(); + }); + }); + } +}); +// [END account_exists_popup] +} diff --git a/auth/package.json b/auth/package.json index a6827027..b4b13f01 100644 --- a/auth/package.json +++ b/auth/package.json @@ -7,6 +7,7 @@ "license": "Apache 2.0", "dependencies": { "firebase": "^8.10.0", + "firebase-admin": "^12.0.0", "firebaseui": "^5.0.0" } } diff --git a/auth/service-worker-sessions.js b/auth/service-worker-sessions.js index babbf91e..05791463 100644 --- a/auth/service-worker-sessions.js +++ b/auth/service-worker-sessions.js @@ -162,3 +162,46 @@ function svcSignInEmail(email, password) { }); // [END auth_svc_sign_in_email] } + +function svcRedirectAdmin() { + const app = { use: (a) => {} }; + + // [START auth_svc_admin] + // Server side code. + const admin = require('firebase-admin'); + + // The Firebase Admin SDK is used here to verify the ID token. + admin.initializeApp(); + + function getIdToken(req) { + // Parse the injected ID token from the request header. + const authorizationHeader = req.headers.authorization || ''; + const components = authorizationHeader.split(' '); + return components.length > 1 ? components[1] : ''; + } + + function checkIfSignedIn(url) { + return (req, res, next) => { + if (req.url == url) { + const idToken = getIdToken(req); + // Verify the ID token using the Firebase Admin SDK. + // User already logged in. Redirect to profile page. + admin.auth().verifyIdToken(idToken).then((decodedClaims) => { + // User is authenticated, user claims can be retrieved from + // decodedClaims. + // In this sample code, authenticated users are always redirected to + // the profile page. + res.redirect('/profile'); + }).catch((error) => { + next(); + }); + } else { + next(); + } + }; + } + + // If a user is signed in, redirect to profile page. + app.use(checkIfSignedIn('/')); + // [END auth_svc_admin] +} diff --git a/auth/twitter.js b/auth/twitter.js index a78370b7..68ed6f35 100644 --- a/auth/twitter.js +++ b/auth/twitter.js @@ -32,7 +32,8 @@ function twitterSignInPopup(provider) { // The signed-in user info. var user = result.user; - // ... + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; @@ -64,6 +65,8 @@ function twitterSignInRedirectResult() { // The signed-in user info. var user = result.user; + // IdP data available in result.additionalUserInfo.profile. + // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; diff --git a/database-next/emulator-suite.js b/database-next/emulator-suite.js index ab86bf9b..f94d3c89 100644 --- a/database-next/emulator-suite.js +++ b/database-next/emulator-suite.js @@ -8,7 +8,7 @@ function onDocumentReady() { const db = getDatabase(); if (location.hostname === "localhost") { // Point to the RTDB emulator running on localhost. - connectDatabaseEmulator(db, "localhost", 9000); + connectDatabaseEmulator(db, "127.0.0.1", 9000); } // [END rtdb_emulator_connect] } diff --git a/database-next/package.json b/database-next/package.json index be3cab88..eb16e2d5 100644 --- a/database-next/package.json +++ b/database-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/database-next/read-and-write.js b/database-next/read-and-write.js index 4196c0d7..c06c66b4 100644 --- a/database-next/read-and-write.js +++ b/database-next/read-and-write.js @@ -44,7 +44,7 @@ function socialListenStarCount() { } // [START rtdb_social_listen_star_count] - const { getDatabase, ref, onValue} = require("firebase/database"); + const { getDatabase, ref, onValue } = require("firebase/database"); const db = getDatabase(); const starCountRef = ref(db, 'posts/' + postId + '/starCount'); diff --git a/database/emulator-suite.js b/database/emulator-suite.js index 8fe739e4..8da23e25 100644 --- a/database/emulator-suite.js +++ b/database/emulator-suite.js @@ -10,7 +10,7 @@ function onDocumentReady() { var db = firebase.database(); if (location.hostname === "localhost") { // Point to the RTDB emulator running on localhost. - db.useEmulator("localhost", 9000); + db.useEmulator("127.0.0.1", 9000); } // [END rtdb_emulator_connect] } diff --git a/firebaseapp-next/package.json b/firebaseapp-next/package.json index d7b654df..13cc74cd 100644 --- a/firebaseapp-next/package.json +++ b/firebaseapp-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/firebaseserverapp-next/firebaseserverapp.js b/firebaseserverapp-next/firebaseserverapp.js new file mode 100644 index 00000000..d0ab338c --- /dev/null +++ b/firebaseserverapp-next/firebaseserverapp.js @@ -0,0 +1,27 @@ +// @ts-nocheck +// [START serverapp_auth] +import { initializeServerApp } from 'firebase/app'; +import { getAuth } from 'firebase/auth'; +import { headers } from 'next/headers'; +import { redirect } from 'next/navigation'; + +export default function MyServerComponent() { + + // Get relevant request headers (in Next.JS) + const authIdToken = headers().get('Authorization')?.split('Bearer ')[1]; + + // Initialize the FirebaseServerApp instance. + const serverApp = initializeServerApp(firebaseConfig, { authIdToken }); + + // Initialize Firebase Authentication using the FirebaseServerApp instance. + const auth = getAuth(serverApp); + + if (auth.currentUser) { + redirect('/profile'); + } + + // ... +} +// [END serverapp_auth] + +const firebaseConfig = {}; diff --git a/firebaseserverapp-next/package.json b/firebaseserverapp-next/package.json new file mode 100644 index 00000000..0836b0b2 --- /dev/null +++ b/firebaseserverapp-next/package.json @@ -0,0 +1,12 @@ +{ + "name": "firebaseserverapp-next", + "version": "1.0.0", + "scripts": { + "compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc" + }, + "license": "Apache-2.0", + "dependencies": { + "firebase": "^10.0.0", + "next": "^14.1.3" + } +} diff --git a/firestore-next/emulator-suite.js b/firestore-next/emulator-suite.js index 30ad24eb..edc900c1 100644 --- a/firestore-next/emulator-suite.js +++ b/firestore-next/emulator-suite.js @@ -7,6 +7,6 @@ function onDocumentReady() { // firebaseApps previously initialized using initializeApp() const db = getFirestore(); - connectFirestoreEmulator(db, 'localhost', 8080); + connectFirestoreEmulator(db, '127.0.0.1', 8080); // [END fs_emulator_connect] } diff --git a/firestore-next/package.json b/firestore-next/package.json index 39b5c6a9..ef3b8708 100644 --- a/firestore-next/package.json +++ b/firestore-next/package.json @@ -6,7 +6,7 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1", + "firebase": "^10.0.0", "geofire-common": "^5.1.0" }, "devDependencies": { diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 2469b736..969c1654 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -583,7 +583,7 @@ describe("firestore", () => { if (docSnap.exists()) { console.log("Document data:", docSnap.data()); } else { - // doc.data() will be undefined in this case + // docSnap.data() will be undefined in this case console.log("No such document!"); } // [END get_document] @@ -683,6 +683,18 @@ describe("firestore", () => { // [END get_multiple_all] }); + it("should get all documents from a subcollection", async () => { + // [START firestore_query_subcollection] + const { collection, getDocs } = require("firebase/firestore"); + // Query a reference to a subcollection + const querySnapshot = await getDocs(collection(db, "cities", "SF", "landmarks")); + querySnapshot.forEach((doc) => { + // doc.data() is never undefined for query doc snapshots + console.log(doc.id, " => ", doc.data()); + }); + // [END firestore_query_subcollection] + }); + it("should listen on multiple documents #UNVERIFIED", (done) => { // [START listen_multiple] const { collection, query, where, onSnapshot } = require("firebase/firestore"); @@ -908,7 +920,7 @@ describe("firestore", () => { // [START in_filter_with_array] const { query, where } = require("firebase/firestore"); - const q = query(citiesRef, where('regions', 'in', [['west_coast', 'east_coast']])); + const q = query(citiesRef, where('regions', 'in', [['west_coast'], ['east_coast']])); // [END in_filter_with_array] } }); diff --git a/firestore/emulator-suite.js b/firestore/emulator-suite.js index 714d38e7..03aef1e9 100644 --- a/firestore/emulator-suite.js +++ b/firestore/emulator-suite.js @@ -6,7 +6,7 @@ function onDocumentReady() { // Firebase previously initialized using firebase.initializeApp(). var db = firebase.firestore(); if (location.hostname === "localhost") { - db.useEmulator("localhost", 8080); + db.useEmulator("127.0.0.1", 8080); } // [END fs_emulator_connect] } diff --git a/firestore/test.firestore.js b/firestore/test.firestore.js index dbf6e18e..20032d36 100644 --- a/firestore/test.firestore.js +++ b/firestore/test.firestore.js @@ -600,7 +600,7 @@ describe("firestore", () => { var docRef = db.collection("cities").doc("SF"); // Valid options for source are 'server', 'cache', or - // 'default'. See https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions + // 'default'. See https://firebase.google.com/docs/reference/js/v8/firebase.firestore.GetOptions // for more information. var getOptions = { source: 'cache' @@ -893,7 +893,7 @@ describe("firestore", () => { // [START in_filter_with_array] citiesRef.where('regions', 'in', - [['west_coast', 'east_coast']]); + [['west_coast'], ['east_coast']]); // [END in_filter_with_array] }); diff --git a/firestore/test.solution-counters.js b/firestore/test.solution-counters.js index c8c62fe6..c7c51cff 100644 --- a/firestore/test.solution-counters.js +++ b/firestore/test.solution-counters.js @@ -22,7 +22,7 @@ function createCounter(ref, num_shards) { // [END create_counter] // [START increment_counter] -function incrementCounter(db, ref, num_shards) { +function incrementCounter(ref, num_shards) { // Select a shard of the counter at random const shard_id = Math.floor(Math.random() * num_shards).toString(); const shard_ref = ref.collection('shards').doc(shard_id); @@ -67,7 +67,7 @@ describe("firestore-solution-counters", () => { // Create a counter, then increment it const ref = db.collection('counters').doc(); return createCounter(ref, 10).then(() => { - return incrementCounter(db, ref, 10); + return incrementCounter(ref, 10); }); }); @@ -75,7 +75,7 @@ describe("firestore-solution-counters", () => { // Create a counter, increment it, then get the count const ref = db.collection('counters').doc(); return createCounter(ref, 10).then(() => { - return incrementCounter(db, ref, 10); + return incrementCounter(ref, 10); }).then(() => { return getCount(ref); }); diff --git a/functions-next/emulator-suite.js b/functions-next/emulator-suite.js index 7a750fc4..db7fd49d 100644 --- a/functions-next/emulator-suite.js +++ b/functions-next/emulator-suite.js @@ -15,7 +15,7 @@ export function emulatorSettings() { const { getFunctions, connectFunctionsEmulator } = require("firebase/functions"); const functions = getFunctions(getApp()); - connectFunctionsEmulator(functions, "localhost", 5001); + connectFunctionsEmulator(functions, "127.0.0.1", 5001); // [END fb_functions_emulator_connect] } diff --git a/functions-next/package.json b/functions-next/package.json index 618ee0c5..14f097ee 100644 --- a/functions-next/package.json +++ b/functions-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/functions/emulator-suite.js b/functions/emulator-suite.js index a9cdc7d7..e0413370 100644 --- a/functions/emulator-suite.js +++ b/functions/emulator-suite.js @@ -3,6 +3,6 @@ import "firebase/functions"; function emulatorSettings() { // [START fb_functions_emulator_connect] - firebase.functions().useEmulator("localhost", 5001); + firebase.functions().useEmulator("127.0.0.1", 5001); // [END fb_functions_emulator_connect] } diff --git a/messaging-next/package.json b/messaging-next/package.json index 2eb86c54..e4887a2a 100644 --- a/messaging-next/package.json +++ b/messaging-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/messaging-next/service-worker.js b/messaging-next/service-worker.js index a81a7c18..55f07524 100644 --- a/messaging-next/service-worker.js +++ b/messaging-next/service-worker.js @@ -40,7 +40,7 @@ function initInSw() { function onBackgroundMessage() { // [START messaging_on_background_message] - const { getMessaging } = require("firebase/messaging"); + const { getMessaging } = require("firebase/messaging/sw"); const { onBackgroundMessage } = require("firebase/messaging/sw"); const messaging = getMessaging(); diff --git a/messaging/service-worker.js b/messaging/service-worker.js index 26f9d097..1a202159 100644 --- a/messaging/service-worker.js +++ b/messaging/service-worker.js @@ -1,5 +1,5 @@ -import firebase from "firebase/app"; -import "firebase/messaging"; +import firebase from 'firebase/app'; +import 'firebase/messaging'; // See: https://github.com/microsoft/TypeScript/issues/14877 /** @type {ServiceWorkerGlobalScope} */ @@ -10,8 +10,9 @@ function initInSw() { // Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here. Other Firebase libraries // are not available in the service worker. - importScripts('https://www.gstatic.com/firebasejs/9.2.0/firebase-app.js'); - importScripts('https://www.gstatic.com/firebasejs/9.2.0/firebase-messaging.js'); + // Replace 10.13.2 with latest version of the Firebase JS SDK. + importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); + importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.js'); // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. @@ -38,16 +39,18 @@ function onBackgroundMessage() { // [START messaging_on_background_message] messaging.onBackgroundMessage((payload) => { - console.log('[firebase-messaging-sw.js] Received background message ', payload); + console.log( + '[firebase-messaging-sw.js] Received background message ', + payload + ); // Customize notification here const notificationTitle = 'Background Message Title'; const notificationOptions = { body: 'Background Message body.', icon: '/firebase-logo.png' }; - - self.registration.showNotification(notificationTitle, - notificationOptions); + + self.registration.showNotification(notificationTitle, notificationOptions); }); // [END messaging_on_background_message] } diff --git a/perf-next/package.json b/perf-next/package.json index e05ef409..688f6db0 100644 --- a/perf-next/package.json +++ b/perf-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/remoteconfig-next/index.js b/remoteconfig-next/index.js index aeeb263a..287f3c99 100644 --- a/remoteconfig-next/index.js +++ b/remoteconfig-next/index.js @@ -14,6 +14,7 @@ function getInstance() { function setMinimumFetchTime() { const remoteConfig = getInstance(); // [START rc_set_minimum_fetch_time] + // The default and recommended production fetch interval for Remote Config is 12 hours remoteConfig.settings.minimumFetchIntervalMillis = 3600000; // [END rc_set_minimum_fetch_time] } diff --git a/remoteconfig-next/package.json b/remoteconfig-next/package.json index ffbd6c3e..952b1540 100644 --- a/remoteconfig-next/package.json +++ b/remoteconfig-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/scripts/separate-snippets.ts b/scripts/separate-snippets.ts index a888d908..b2f41de3 100644 --- a/scripts/separate-snippets.ts +++ b/scripts/separate-snippets.ts @@ -18,6 +18,15 @@ const RE_END_SNIPPET = /\[END\s+([A-Za-z_]+)\s*\]/; // TODO: Handle multiline imports? const RE_REQUIRE = /const {(.+?)} = require\((.+?)\)/; +// Regex for ref docs URLs +// eg. "https://firebase.google.com/docs/reference/js/v8/firebase.User" +const RE_REF_DOCS = /https:\/\/firebase\.google\.com\/docs\/reference\/js\/(.*)/; + +// Maps v8 ref docs URLs to their v9 counterpart +const REF_DOCS_MAPPINGS: { [key: string]: string } = { + "v8/firebase.User" : "auth.user" +}; + type SnippetsConfig = { enabled: boolean; suffix: string; @@ -30,6 +39,23 @@ function isBlank(line: string) { return line.trim().length === 0; } +/** + * Replace all v8 ref doc urls with their v9 counterpart. + */ +function replaceRefDocsUrls(lines: string[]) { + const outputLines = []; + for (const line of lines) { + if (line.match(RE_REF_DOCS)) { + outputLines.push(line.replace(RE_REF_DOCS, (match: string, p1?: string) => { + return p1 ? `https://firebase.google.com/docs/reference/js/${REF_DOCS_MAPPINGS[p1]}` : match; + })); + } else { + outputLines.push(line); + } + } + return outputLines; +} + /** * Replace all const { foo } = require('bar') with import { foo } from 'bar'; */ @@ -119,6 +145,7 @@ function processSnippet( outputLines = addSuffixToSnippetNames(outputLines, snippetSuffix); outputLines = adjustIndentation(outputLines); outputLines = removeFirstLineAfterComments(outputLines); + outputLines = replaceRefDocsUrls(outputLines); // Add a preamble to every snippet const preambleLines = [ diff --git a/snippets/auth-next/apple/auth_apple_signin_popup.js b/snippets/auth-next/apple/auth_apple_signin_popup.js index c2a392cf..e3a0f11b 100644 --- a/snippets/auth-next/apple/auth_apple_signin_popup.js +++ b/snippets/auth-next/apple/auth_apple_signin_popup.js @@ -18,6 +18,7 @@ signInWithPopup(auth, provider) const accessToken = credential.accessToken; const idToken = credential.idToken; + // IdP data available using getAdditionalUserInfo(result) // ... }) .catch((error) => { diff --git a/snippets/auth-next/custom-email-handler/auth_handle_mgmt_query_params.js b/snippets/auth-next/custom-email-handler/auth_handle_mgmt_query_params.js index 6dec3399..d0a22696 100644 --- a/snippets/auth-next/custom-email-handler/auth_handle_mgmt_query_params.js +++ b/snippets/auth-next/custom-email-handler/auth_handle_mgmt_query_params.js @@ -23,8 +23,8 @@ document.addEventListener('DOMContentLoaded', () => { // Configure the Firebase SDK. // This is the minimum configuration required for the API to be used. const config = { - 'apiKey': "YOU_API_KEY" // Copy this key from the web initialization - // snippet found in the Firebase console. + 'apiKey': "YOUR_API_KEY" // Copy this key from the web initialization + // snippet found in the Firebase console. }; const app = initializeApp(config); const auth = getAuth(app); diff --git a/snippets/auth-next/email-link-auth/auth_email_link_actioncode_settings.js b/snippets/auth-next/email-link-auth/auth_email_link_actioncode_settings.js index a1480a11..010b5c09 100644 --- a/snippets/auth-next/email-link-auth/auth_email_link_actioncode_settings.js +++ b/snippets/auth-next/email-link-auth/auth_email_link_actioncode_settings.js @@ -19,6 +19,7 @@ const actionCodeSettings = { installApp: true, minimumVersion: '12' }, - dynamicLinkDomain: 'example.page.link' + // The domain must be configured in Firebase Hosting and owned by the project. + linkDomain: 'custom-domain.com' }; // [END auth_email_link_actioncode_settings_modular] \ No newline at end of file diff --git a/snippets/auth-next/email-link-auth/email_link_complete.js b/snippets/auth-next/email-link-auth/email_link_complete.js index b42b9af9..dbe1143d 100644 --- a/snippets/auth-next/email-link-auth/email_link_complete.js +++ b/snippets/auth-next/email-link-auth/email_link_complete.js @@ -26,11 +26,13 @@ if (isSignInWithEmailLink(auth, window.location.href)) { .then((result) => { // Clear email from storage. window.localStorage.removeItem('emailForSignIn'); - // You can access the new user via result.user - // Additional user info profile not available via: - // result.additionalUserInfo.profile == null + // You can access the new user by importing getAdditionalUserInfo + // and calling it with result: + // getAdditionalUserInfo(result) + // You can access the user's profile via: + // getAdditionalUserInfo(result)?.profile // You can check if the user is new or existing: - // result.additionalUserInfo.isNewUser + // getAdditionalUserInfo(result)?.isNewUser }) .catch((error) => { // Some error occurred, you can inspect the code: error.code diff --git a/snippets/auth-next/email/auth_signup_password.js b/snippets/auth-next/email/auth_signup_password.js index 00aa9bd5..936b64ce 100644 --- a/snippets/auth-next/email/auth_signup_password.js +++ b/snippets/auth-next/email/auth_signup_password.js @@ -10,7 +10,7 @@ import { getAuth, createUserWithEmailAndPassword } from "firebase/auth"; const auth = getAuth(); createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { - // Signed in + // Signed up const user = userCredential.user; // ... }) diff --git a/snippets/auth-next/emulator-suite/auth_emulator_connect.js b/snippets/auth-next/emulator-suite/auth_emulator_connect.js index dd0b2e37..525a92b8 100644 --- a/snippets/auth-next/emulator-suite/auth_emulator_connect.js +++ b/snippets/auth-next/emulator-suite/auth_emulator_connect.js @@ -8,5 +8,5 @@ import { getAuth, connectAuthEmulator } from "firebase/auth"; const auth = getAuth(); -connectAuthEmulator(auth, "http://localhost:9099"); +connectAuthEmulator(auth, "http://127.0.0.1:9099"); // [END auth_emulator_connect_modular] \ No newline at end of file diff --git a/snippets/auth-next/facebook/auth_facebook_signin_popup.js b/snippets/auth-next/facebook/auth_facebook_signin_popup.js index 7d35aaf2..524a11ac 100644 --- a/snippets/auth-next/facebook/auth_facebook_signin_popup.js +++ b/snippets/auth-next/facebook/auth_facebook_signin_popup.js @@ -17,6 +17,7 @@ signInWithPopup(auth, provider) const credential = FacebookAuthProvider.credentialFromResult(result); const accessToken = credential.accessToken; + // IdP data available using getAdditionalUserInfo(result) // ... }) .catch((error) => { diff --git a/snippets/auth-next/facebook/auth_facebook_signin_redirect_result.js b/snippets/auth-next/facebook/auth_facebook_signin_redirect_result.js index eb6cdb30..c65c47d7 100644 --- a/snippets/auth-next/facebook/auth_facebook_signin_redirect_result.js +++ b/snippets/auth-next/facebook/auth_facebook_signin_redirect_result.js @@ -15,6 +15,8 @@ getRedirectResult(auth) const token = credential.accessToken; const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/snippets/auth-next/github/auth_github_signin_popup.js b/snippets/auth-next/github/auth_github_signin_popup.js index e1b4768e..3839a70f 100644 --- a/snippets/auth-next/github/auth_github_signin_popup.js +++ b/snippets/auth-next/github/auth_github_signin_popup.js @@ -16,6 +16,7 @@ signInWithPopup(auth, provider) // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. diff --git a/snippets/auth-next/github/auth_github_signin_redirect_result.js b/snippets/auth-next/github/auth_github_signin_redirect_result.js index b3d31d0a..fb5f7ca2 100644 --- a/snippets/auth-next/github/auth_github_signin_redirect_result.js +++ b/snippets/auth-next/github/auth_github_signin_redirect_result.js @@ -19,6 +19,8 @@ getRedirectResult(auth) // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/snippets/auth-next/google-signin/auth_google_signin_popup.js b/snippets/auth-next/google-signin/auth_google_signin_popup.js index d6c70eb9..433bb88e 100644 --- a/snippets/auth-next/google-signin/auth_google_signin_popup.js +++ b/snippets/auth-next/google-signin/auth_google_signin_popup.js @@ -15,6 +15,7 @@ signInWithPopup(auth, provider) const token = credential.accessToken; // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. diff --git a/snippets/auth-next/google-signin/auth_google_signin_redirect_result.js b/snippets/auth-next/google-signin/auth_google_signin_redirect_result.js index 50c4aded..2953ab5f 100644 --- a/snippets/auth-next/google-signin/auth_google_signin_redirect_result.js +++ b/snippets/auth-next/google-signin/auth_google_signin_redirect_result.js @@ -16,6 +16,8 @@ getRedirectResult(auth) // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/snippets/auth-next/index/auth_current_user.js b/snippets/auth-next/index/auth_current_user.js index c094a464..6925b021 100644 --- a/snippets/auth-next/index/auth_current_user.js +++ b/snippets/auth-next/index/auth_current_user.js @@ -12,7 +12,7 @@ const user = auth.currentUser; if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/auth.user // ... } else { // No user is signed in. diff --git a/snippets/auth-next/index/auth_set_language_code.js b/snippets/auth-next/index/auth_set_language_code.js index 47e78295..fe9a2025 100644 --- a/snippets/auth-next/index/auth_set_language_code.js +++ b/snippets/auth-next/index/auth_set_language_code.js @@ -10,5 +10,5 @@ import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. -// firebase.auth().useDeviceLanguage(); +// auth.useDeviceLanguage(); // [END auth_set_language_code_modular] \ No newline at end of file diff --git a/snippets/auth-next/index/auth_state_listener.js b/snippets/auth-next/index/auth_state_listener.js index aebe6818..0f3e4e88 100644 --- a/snippets/auth-next/index/auth_state_listener.js +++ b/snippets/auth-next/index/auth_state_listener.js @@ -11,7 +11,7 @@ const auth = getAuth(); onAuthStateChanged(auth, (user) => { if (user) { // User is signed in, see docs for a list of available properties - // https://firebase.google.com/docs/reference/js/firebase.User + // https://firebase.google.com/docs/reference/js/auth.user const uid = user.uid; // ... } else { diff --git a/snippets/auth-next/link-multiple-accounts/account_exists_popup.js b/snippets/auth-next/link-multiple-accounts/account_exists_popup.js new file mode 100644 index 00000000..2c667882 --- /dev/null +++ b/snippets/auth-next/link-multiple-accounts/account_exists_popup.js @@ -0,0 +1,61 @@ +// This snippet file was generated by processing the source file: +// ./auth-next/link-multiple-accounts.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + + // [START account_exists_popup_modular] + import { signInWithPopup, signInWithEmailAndPassword, linkWithCredential } from "firebase/auth"; + + // User tries to sign in with Facebook. + signInWithPopup(auth, facebookProvider).catch((error) => { + // User's email already exists. + if (error.code === 'auth/account-exists-with-different-credential') { + // The pending Facebook credential. + const pendingCred = error.credential; + // The provider account's email address. + const email = error.customData.email; + + // Present the user with a list of providers they might have + // used to create the original account. + // Then, ask the user to sign in with the existing provider. + const method = promptUserForSignInMethod(); + + if (method === 'password') { + // TODO: Ask the user for their password. + // In real scenario, you should handle this asynchronously. + const password = promptUserForPassword(); + signInWithEmailAndPassword(auth, email, password).then((result) => { + return linkWithCredential(result.user, pendingCred); + }).then(() => { + // Facebook account successfully linked to the existing user. + goToApp(); + }); + return; + } + + // All other cases are external providers. + // Construct provider object for that provider. + // TODO: Implement getProviderForProviderId. + const provider = getProviderForProviderId(method); + // At this point, you should let the user know that they already have an + // account with a different provider, and validate they want to sign in + // with the new provider. + // Note: Browsers usually block popups triggered asynchronously, so in + // real app, you should ask the user to click on a "Continue" button + // that will trigger signInWithPopup(). + signInWithPopup(auth, provider).then((result) => { + // Note: Identity Platform doesn't control the provider's sign-in + // flow, so it's possible for the user to sign in with an account + // with a different email from the first one. + + // Link the Facebook credential. We have access to the pending + // credential, so we can directly call the link method. + linkWithCredential(result.user, pendingCred).then((userCred) => { + // Success. + goToApp(); + }); + }); + } +}); +// [END account_exists_popup_modular] \ No newline at end of file diff --git a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_invisible.js b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_invisible.js index f6b6893f..7fd16eea 100644 --- a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_invisible.js +++ b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_invisible.js @@ -8,11 +8,11 @@ import { getAuth, RecaptchaVerifier } from "firebase/auth"; const auth = getAuth(); -window.recaptchaVerifier = new RecaptchaVerifier('sign-in-button', { +window.recaptchaVerifier = new RecaptchaVerifier(auth, 'sign-in-button', { 'size': 'invisible', 'callback': (response) => { // reCAPTCHA solved, allow signInWithPhoneNumber. onSignInSubmit(); } -}, auth); +}); // [END auth_phone_recaptcha_verifier_invisible_modular] \ No newline at end of file diff --git a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_simple.js b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_simple.js index ed56fe99..b57eb37b 100644 --- a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_simple.js +++ b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_simple.js @@ -8,5 +8,5 @@ import { getAuth, RecaptchaVerifier } from "firebase/auth"; const auth = getAuth(); -window.recaptchaVerifier = new RecaptchaVerifier('recaptcha-container', {}, auth); +window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', {}); // [END auth_phone_recaptcha_verifier_simple_modular] \ No newline at end of file diff --git a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_visible.js b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_visible.js index c91e6041..42ebbe69 100644 --- a/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_visible.js +++ b/snippets/auth-next/phone-auth/auth_phone_recaptcha_verifier_visible.js @@ -8,7 +8,7 @@ import { getAuth, RecaptchaVerifier } from "firebase/auth"; const auth = getAuth(); -window.recaptchaVerifier = new RecaptchaVerifier('recaptcha-container', { +window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', { 'size': 'normal', 'callback': (response) => { // reCAPTCHA solved, allow signInWithPhoneNumber. @@ -18,5 +18,5 @@ window.recaptchaVerifier = new RecaptchaVerifier('recaptcha-container', { // Response expired. Ask user to solve reCAPTCHA again. // ... } -}, auth); +}); // [END auth_phone_recaptcha_verifier_visible_modular] \ No newline at end of file diff --git a/snippets/auth-next/twitter/auth_twitter_signin_popup.js b/snippets/auth-next/twitter/auth_twitter_signin_popup.js index ae8e2007..adb7322e 100644 --- a/snippets/auth-next/twitter/auth_twitter_signin_popup.js +++ b/snippets/auth-next/twitter/auth_twitter_signin_popup.js @@ -18,6 +18,7 @@ signInWithPopup(auth, provider) // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. diff --git a/snippets/auth-next/twitter/auth_twitter_signin_redirect_result.js b/snippets/auth-next/twitter/auth_twitter_signin_redirect_result.js index 73e42bab..357faa4e 100644 --- a/snippets/auth-next/twitter/auth_twitter_signin_redirect_result.js +++ b/snippets/auth-next/twitter/auth_twitter_signin_redirect_result.js @@ -19,6 +19,8 @@ getRedirectResult(auth) // The signed-in user info. const user = result.user; + // IdP data available using getAdditionalUserInfo(result) + // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; diff --git a/snippets/database-next/emulator-suite/rtdb_emulator_connect.js b/snippets/database-next/emulator-suite/rtdb_emulator_connect.js index 4cc268e7..0bd145f6 100644 --- a/snippets/database-next/emulator-suite/rtdb_emulator_connect.js +++ b/snippets/database-next/emulator-suite/rtdb_emulator_connect.js @@ -10,6 +10,6 @@ import { getDatabase, connectDatabaseEmulator } from "firebase/database"; const db = getDatabase(); if (location.hostname === "localhost") { // Point to the RTDB emulator running on localhost. - connectDatabaseEmulator(db, "localhost", 9000); + connectDatabaseEmulator(db, "127.0.0.1", 9000); } // [END rtdb_emulator_connect_modular] \ No newline at end of file diff --git a/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js b/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js index ab520ce6..eb782894 100644 --- a/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js +++ b/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js @@ -5,7 +5,7 @@ // 'npm run snippets'. // [START rtdb_social_listen_star_count_modular] -import { getDatabase, ref, onValue} from "firebase/database"; +import { getDatabase, ref, onValue } from "firebase/database"; const db = getDatabase(); const starCountRef = ref(db, 'posts/' + postId + '/starCount'); diff --git a/snippets/firestore-next/emulator-suite/fs_emulator_connect.js b/snippets/firestore-next/emulator-suite/fs_emulator_connect.js index 7e31fcfd..d70fbb68 100644 --- a/snippets/firestore-next/emulator-suite/fs_emulator_connect.js +++ b/snippets/firestore-next/emulator-suite/fs_emulator_connect.js @@ -9,5 +9,5 @@ import { getFirestore, connectFirestoreEmulator } from "firebase/firestore"; // firebaseApps previously initialized using initializeApp() const db = getFirestore(); -connectFirestoreEmulator(db, 'localhost', 8080); +connectFirestoreEmulator(db, '127.0.0.1', 8080); // [END fs_emulator_connect_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/firestore_query_subcollection.js b/snippets/firestore-next/test-firestore/firestore_query_subcollection.js new file mode 100644 index 00000000..76a1b847 --- /dev/null +++ b/snippets/firestore-next/test-firestore/firestore_query_subcollection.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START firestore_query_subcollection_modular] +import { collection, getDocs } from "firebase/firestore"; +// Query a reference to a subcollection +const querySnapshot = await getDocs(collection(db, "cities", "SF", "landmarks")); +querySnapshot.forEach((doc) => { + // doc.data() is never undefined for query doc snapshots + console.log(doc.id, " => ", doc.data()); +}); +// [END firestore_query_subcollection_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/get_document.js b/snippets/firestore-next/test-firestore/get_document.js index 4cfbaaf6..1653b61a 100644 --- a/snippets/firestore-next/test-firestore/get_document.js +++ b/snippets/firestore-next/test-firestore/get_document.js @@ -13,7 +13,7 @@ const docSnap = await getDoc(docRef); if (docSnap.exists()) { console.log("Document data:", docSnap.data()); } else { - // doc.data() will be undefined in this case + // docSnap.data() will be undefined in this case console.log("No such document!"); } // [END get_document_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/in_filter_with_array.js b/snippets/firestore-next/test-firestore/in_filter_with_array.js index ebe6f18b..190e9dfe 100644 --- a/snippets/firestore-next/test-firestore/in_filter_with_array.js +++ b/snippets/firestore-next/test-firestore/in_filter_with_array.js @@ -7,5 +7,5 @@ // [START in_filter_with_array_modular] import { query, where } from "firebase/firestore"; -const q = query(citiesRef, where('regions', 'in', [['west_coast', 'east_coast']])); +const q = query(citiesRef, where('regions', 'in', [['west_coast'], ['east_coast']])); // [END in_filter_with_array_modular] \ No newline at end of file diff --git a/snippets/functions-next/emulator-suite/fb_functions_emulator_connect.js b/snippets/functions-next/emulator-suite/fb_functions_emulator_connect.js index 9b186fc2..835a2c60 100644 --- a/snippets/functions-next/emulator-suite/fb_functions_emulator_connect.js +++ b/snippets/functions-next/emulator-suite/fb_functions_emulator_connect.js @@ -9,5 +9,5 @@ import { getApp } from "firebase/app"; import { getFunctions, connectFunctionsEmulator } from "firebase/functions"; const functions = getFunctions(getApp()); -connectFunctionsEmulator(functions, "localhost", 5001); +connectFunctionsEmulator(functions, "127.0.0.1", 5001); // [END fb_functions_emulator_connect_modular] \ No newline at end of file diff --git a/snippets/messaging-next/service-worker/messaging_on_background_message.js b/snippets/messaging-next/service-worker/messaging_on_background_message.js index b7f65b53..79c17eea 100644 --- a/snippets/messaging-next/service-worker/messaging_on_background_message.js +++ b/snippets/messaging-next/service-worker/messaging_on_background_message.js @@ -5,7 +5,7 @@ // 'npm run snippets'. // [START messaging_on_background_message_modular] -import { getMessaging } from "firebase/messaging"; +import { getMessaging } from "firebase/messaging/sw"; import { onBackgroundMessage } from "firebase/messaging/sw"; const messaging = getMessaging(); diff --git a/snippets/remoteconfig-next/index/rc_set_minimum_fetch_time.js b/snippets/remoteconfig-next/index/rc_set_minimum_fetch_time.js index 131976c5..77aa9f45 100644 --- a/snippets/remoteconfig-next/index/rc_set_minimum_fetch_time.js +++ b/snippets/remoteconfig-next/index/rc_set_minimum_fetch_time.js @@ -5,5 +5,6 @@ // 'npm run snippets'. // [START rc_set_minimum_fetch_time_modular] +// The default and recommended production fetch interval for Remote Config is 12 hours remoteConfig.settings.minimumFetchIntervalMillis = 3600000; // [END rc_set_minimum_fetch_time_modular] \ No newline at end of file diff --git a/snippets/storage-next/emulator-suite/storage_emulator_connect.js b/snippets/storage-next/emulator-suite/storage_emulator_connect.js new file mode 100644 index 00000000..4b247d17 --- /dev/null +++ b/snippets/storage-next/emulator-suite/storage_emulator_connect.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./storage-next/emulator-suite.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START storage_emulator_connect_modular] +import { getStorage, connectStorageEmulator } from "firebase/storage"; + +const storage = getStorage(); +if (location.hostname === "localhost") { + // Point to the Storage emulator running on localhost. + connectStorageEmulator(storage, "127.0.0.1", 9199); +} +// [END storage_emulator_connect_modular] \ No newline at end of file diff --git a/storage-next/emulator-suite.js b/storage-next/emulator-suite.js new file mode 100644 index 00000000..78891a4f --- /dev/null +++ b/storage-next/emulator-suite.js @@ -0,0 +1,14 @@ +// [SNIPPET_REGISTRY disabled] +// [SNIPPETS_SEPARATION enabled] + +function onDocumentReady() { + // [START storage_emulator_connect] + const { getStorage, connectStorageEmulator } = require("firebase/storage"); + + const storage = getStorage(); + if (location.hostname === "localhost") { + // Point to the Storage emulator running on localhost. + connectStorageEmulator(storage, "127.0.0.1", 9199); + } + // [END storage_emulator_connect] +} diff --git a/storage-next/package.json b/storage-next/package.json index a64d67b2..b1c449ab 100644 --- a/storage-next/package.json +++ b/storage-next/package.json @@ -6,6 +6,6 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^9.12.1" + "firebase": "^10.0.0" } } diff --git a/storage/emulator-suite.js b/storage/emulator-suite.js new file mode 100644 index 00000000..be1f2dd4 --- /dev/null +++ b/storage/emulator-suite.js @@ -0,0 +1,16 @@ +// These samples are intended for Web so this import would normally be +// done in HTML however using modules here is more convenient for +// ensuring sample correctness offline. +import firebase from "firebase/app"; +import "firebase/storage"; + +function onDocumentReady() { + // [START storage_emulator_connect] + var storage = firebase.storage(); + if (location.hostname === "localhost") { + // Point to the Storage emulator running on localhost. + storage.useEmulator("127.0.0.1", 9199); + } + // [END storage_emulator_connect] +} +