Firebase Authentication

На этом уроке нам не понадобятся другие классы мы будем только работать с классом LogoActivity. 

LogoActivity

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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

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;

public class LoginActivity extends AppCompatActivity {
private EditText edLogin, edPassword;
private FirebaseAuth mAuth;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
init();

}

@Override
protected void onStart() {
super.onStart();
FirebaseUser cUser = mAuth.getCurrentUser();
if(cUser != null)
{
Toast.makeText(this, "User not null", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "User null", Toast.LENGTH_SHORT).show();
}
}

private void init()
{
edLogin = findViewById(R.id.edLogin);
edPassword = findViewById(R.id.edPassword);
mAuth = FirebaseAuth.getInstance();
}
public void onClickSignUp(View view)
{
if(!TextUtils.isEmpty(edLogin.getText().toString()) && !TextUtils.isEmpty(edPassword.getText().toString()))
{
mAuth.createUserWithEmailAndPassword(edLogin.getText().toString(),edPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(), "User SignUp Successful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "User SignUp failed", Toast.LENGTH_SHORT).show();
}
}
});

}
else
{
Toast.makeText(getApplicationContext(), "Please entre Email and Password", Toast.LENGTH_SHORT).show();
}
}
public void onClickSignIn(View view)
{
if(!TextUtils.isEmpty(edLogin.getText().toString()) && !TextUtils.isEmpty(edPassword.getText().toString())){
mAuth.signInWithEmailAndPassword(edLogin.getText().toString(),edPassword.getText().toString()).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(), "User SignIn Successful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "User SignIn failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}

login_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/edLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="32dp"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/edLogin"
app:layout_constraintStart_toStartOf="@+id/edLogin"
app:layout_constraintTop_toBottomOf="@+id/edLogin" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:onClick="onClickSignUp"
android:text="Зарегистрироватся"
app:layout_constraintEnd_toEndOf="@+id/edPassword"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="@+id/edPassword"
app:layout_constraintTop_toBottomOf="@+id/edPassword" />

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:onClick="onClickSignIn"
android:text="Войти"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button3" />
</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *