add firebase to react code example

Example 1: install firebase in react

$ npm install firebase

# or

$ yarn add firebase

Example 2: add firebase in react

import * as firebase from 'firebase'

const firebaseConfig = {
    apiKey: "AIzaSyC6EDL8gMkZc3GGzGveMqWe5zvAr5DNiL4",
    authDomain: "blood-components.firebaseapp.com",
    databaseURL: "https://blood-components.firebaseio.com",
    projectId: "blood-components",
    storageBucket: "blood-components.appspot.com",
    messagingSenderId: "388223113819",
    appId: "1:388223113819:web:1273570a12add0fedafd7e",
    measurementId: "G-K6NM078FWD"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();

//initiating references to the databases
const usersRef = db.collection('users')

// for privileges purposes
const functions = firebase.functions();

//google provider sign-in
const googleProvider = new firebase.auth.GoogleAuthProvider();


export { firebase, db, auth, functions, googleProvider }

Example 3: firebase react js

npm install firebase

Example 4: firebase connecten

package com.example.phonechat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

public class RegistrationActivity extends AppCompatActivity {


    private Button mRegistration;
    private EditText mEmail, mPassowrd, mName;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user!=null) {
                    Intent intent = new Intent(getApplication(), MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                    return;
                }

            }
        };
        mAuth = FirebaseAuth.getInstance();

        mRegistration = findViewById(R.id.registration);
        mEmail = findViewById(R.id.gmail);
        mName = findViewById(R.id.name);
        mPassowrd = findViewById(R.id.password);


        mRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = mName.getText().toString();
                final String email = mEmail.getText().toString();
                final String password = mPassowrd.getText().toString();
                mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(activity: getApplication(), new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(getApplicationContext(), "Sign in ERROR", Toast.LENGTH_SHORT).show();

                        }else{
                            String userId = mAuth.getCurrentUser().getUid();
                            DatabaseRefere currentUSerDb = FirebaseDatabase.getInstace().getRefernce().child("users").child(userId);

                            Map userinfo = new HashMap<>();
                            userInfo.put( k: "email", email);
                            userInfo.put( k: "name", name);
                            userInfo.put( k: "profileImageErl", v: "default");

                            currentUSerDb.updateChilderen(userInfo);

                        }
                    }
                });
            }
        });

    }
    @Override
    protected void  onStart() {
        super.onStart();
        mAuth.addAuthStateListener(firebaseAuthStateListener);


    }
    @Override
    protected void onStop() {
        super.onStop();
        mAuth.removeAuthStateListener(firebaseAuthStateListener);

    }
}

Tags: