MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnRegister;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
RelativeLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = findViewById(R.id.btnSignIn);
btnRegister = findViewById(R.id.btnRegister);
root = findViewById(R.id.root_element);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("users");
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegisterWindoW();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSignInWindow();
}
});
}
private void showSignInWindow() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Войти");
dialog.setMessage("Введите данные для входа");
LayoutInflater inflater = LayoutInflater.from(this);
View sign_in_window = inflater.inflate(R.layout.sign_in_window, null);
dialog.setView(sign_in_window);
final MaterialEditText email = sign_in_window.findViewById(R.id.emailField);
final MaterialEditText pass = sign_in_window.findViewById(R.id.passField);
dialog.setNegativeButton("Отменить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
dialog.setPositiveButton("Войти", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (TextUtils.isEmpty(email.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (pass.getText().toString().length() < 5) {
Snackbar.make(root, "Пароль должен быть более 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
auth.signInWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivity.this, MapiActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(root, "Ошибка авторизации" + e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
});
}
});
dialog.show();
}
private void showRegisterWindoW() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Зарегистрироваться");
dialog.setMessage("Введите данные для регистрации");
LayoutInflater inflater = LayoutInflater.from(this);
View register_window = inflater.inflate(R.layout.register_window, null);
dialog.setView(register_window);
final MaterialEditText email = register_window.findViewById(R.id.emailField);
final MaterialEditText pass = register_window.findViewById(R.id.passField);
final MaterialEditText name = register_window.findViewById(R.id.nameField);
final MaterialEditText mname = register_window.findViewById(R.id.mnameField);
final MaterialEditText lname = register_window.findViewById(R.id.lnameField);
final MaterialEditText klass = register_window.findViewById(R.id.klassField);
dialog.setNegativeButton("Отменить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
dialog.setPositiveButton("Добавить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (TextUtils.isEmpty(email.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (pass.getText().toString().length() < 5) {
Snackbar.make(root, "Пароль должен быть более 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(name.getText().toString())) {
Snackbar.make(root, "Введите ваше имя", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(mname.getText().toString())) {
Snackbar.make(root, "Введите вашу фамилию", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(lname.getText().toString())) {
Snackbar.make(root, "Введите ваше отчество", Snackbar.LENGTH_SHORT).show();
return;
}
//регистрация пользователя
auth.createUserWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
User user = new User();
user.setEmail(email.getText().toString());
user.setName(name.getText().toString());
user.setMname(mname.getText().toString());
user.setLname(lname.getText().toString());
user.setPass(pass.getText().toString());
user.setKlass(klass.getText().toString());
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(root, "Пользователь добавлен", Snackbar.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(root, "Ошибка регистрации." + e.getMessage(),Snackbar.LENGTH_LONG).show();
}
});
}
});
dialog.show();
}
}