const auth = firebase.auth();
auth.createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
auth.signInWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
auth.signOut().then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
const user = auth.currentUser;
auth.onAuthStateChanged((user) => {
if (user) {
// User is signed in
} else {
// User is signed out
}
});
const database = firebase.database();
database.ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
database.ref('/users/' + userId).once('value').then((snapshot) => {
const username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
});
database.ref('/users/' + userId).on('value', (snapshot) => {
const data = snapshot.val();
updateStarCount(postElement, data);
});
database.ref('users/' + userId).update({
username: newUsername
});
database.ref('users/' + userId).remove();
const db = firebase.firestore();
db.collection("users").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});
db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA"
});
db.collection("cities").doc("LA")
.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
db.collection("cities").doc("LA")
.onSnapshot((doc) => {
console.log("Current data: ", doc.data());
});
db.collection("cities").where("capital", "==", true)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
});
const storage = firebase.storage();
const ref = storage.ref('images/mountains.jpg');
const file = ... // Use a Blob or File API to get the file
ref.put(file).then((snapshot) => {
console.log('Uploaded a file!');
});
storage.ref('images/mountains.jpg').getDownloadURL()
.then((url) => {
// Insert url into an <img> tag to "download"
});
const ref = storage.ref('images/mountains.jpg');
ref.delete().then(() => {
// File deleted successfully
}).catch((error) => {
// An error occurred!
});
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate((snap, context) => {
// Get an object representing the document
const newValue = snap.data();
// access a particular field as you would any JS property
const name = newValue.name;
// perform desired operations ...
});
firebase init hosting
firebase deploy --only hosting
const messaging = firebase.messaging();
messaging.requestPermission()
.then(() => {
console.log('Notification permission granted.');
return messaging.getToken();
})
.then((token) => {
console.log('Token:', token);
})
.catch((err) => {
console.log('Unable to get permission to notify.', err);
});
messaging.onMessage((payload) => {
console.log('Message received. ', payload);
});
const analytics = firebase.analytics();
analytics.logEvent('select_content', {
content_type: 'image',
content_id: 'P12453'
});
analytics.setUserProperties({favorite_food: 'pizza'});
const perf = firebase.performance();
const trace = perf.trace('test_trace');
trace.start();
// Do some time-consuming work...
trace.stop();
const remoteConfig = firebase.remoteConfig();
remoteConfig.fetchAndActivate()
.then(() => {
const welcomeMessage = remoteConfig.getValue('welcome_message');
console.log(welcomeMessage.asString());
})
.catch((err) => {
console.error('Error fetching remote config.', err);
});
2024 © All rights reserved - buraxta.com