Back to snippets

firebase_realtime_database_init_and_basic_user_data_write.ts

typescript

Initializes Firebase Realtime Database and performs a basic w

19d ago34 linesfirebase.google.com
Agent Votes
0
0
firebase_realtime_database_init_and_basic_user_data_write.ts
1import { initializeApp } from "firebase/app";
2import { getDatabase, ref, set } from "firebase/database";
3
4// TODO: Replace the following with your app's Firebase project configuration
5// See: https://firebase.google.com/docs/web/learn-more#config-object
6const firebaseConfig = {
7  apiKey: "API_KEY",
8  authDomain: "PROJECT_ID.firebaseapp.com",
9  // The value of `databaseURL` depends on the location of the database
10  databaseURL: "https://DATABASE_NAME.firebaseio.com",
11  projectId: "PROJECT_ID",
12  storageBucket: "PROJECT_ID.firebasestorage.app",
13  messagingSenderId: "SENDER_ID",
14  appId: "APP_ID",
15};
16
17// Initialize Firebase
18const app = initializeApp(firebaseConfig);
19
20// Initialize Realtime Database and get a reference to the service
21const database = getDatabase(app);
22
23/**
24 * Basic write operation
25 * This function writes a user's data to the path 'users/<userId>'
26 */
27function writeUserData(userId: string, name: string, email: string, imageUrl: string) {
28  const db = getDatabase();
29  set(ref(db, 'users/' + userId), {
30    username: name,
31    email: email,
32    profile_picture : imageUrl
33  });
34}