Answers for "firebase setup :firebase"

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<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);

    }
}
Posted by: Guest on February-03-2020

Browse Popular Code Answers by Language