firebase getUser code example

Example 1: firebase auth get current user

var user = firebase.auth().currentUser;var name, email, photoUrl, uid, emailVerified;if (user != null) {  name = user.displayName;  email = user.email;  photoUrl = user.photoURL;  emailVerified = user.emailVerified;  uid = user.uid;  // The user's ID, unique to the Firebase project. Do NOT use                   // this value to authenticate with your backend server, if                   // you have one. Use User.getToken() instead.}

Example 2: 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() {
                    @Override
                    public void onComplete(@NonNull Task 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);

    }
}

Example 3: firebase.auth.currentuser

var user = firebase.auth().currentUser;if (user) {  // User is signed in.} else {  // No user is signed in.}

Tags:

Misc Example