Login e Criar conta
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.example.lifegrid;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class CriarContaActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
EdgeToEdge.enable(this);
|
||||
setContentView(R.layout.activity_criar_conta);
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
|
||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
package com.example.lifegrid;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Patterns;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
@@ -8,8 +17,26 @@ import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
private TextView textView5;
|
||||
private TextView textView1;
|
||||
private TextView criarContaTextView;
|
||||
private TextView emailTextView;
|
||||
private EditText emailEditText;
|
||||
private TextView passTextView;
|
||||
private EditText passwordEditText;
|
||||
private TextView passesquecerTextView;
|
||||
private Button loginButton;
|
||||
private Button googleButton;
|
||||
private TextView ouTextView;
|
||||
private ProgressBar loadingProgressBar;
|
||||
private FirebaseAuth firebaseAuth;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -20,6 +47,126 @@ public class LoginActivity extends AppCompatActivity {
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
textView5 = findViewById(R.id.textView5);
|
||||
textView1 = findViewById(R.id.textView1);
|
||||
criarContaTextView = findViewById(R.id.criarContaTextView);
|
||||
emailTextView = findViewById(R.id.emailTextView);
|
||||
emailEditText = findViewById(R.id.emailEditText);
|
||||
passTextView = findViewById(R.id.passTextView);
|
||||
passwordEditText = findViewById(R.id.passwordEditText);
|
||||
passesquecerTextView = findViewById(R.id.passesquecerTextView);
|
||||
loginButton = findViewById(R.id.loginButton);
|
||||
googleButton = findViewById(R.id.googleButton);
|
||||
ouTextView = findViewById(R.id.ouTextView);
|
||||
loadingProgressBar = findViewById(R.id.loadingProgressBar);
|
||||
|
||||
FirebaseApp.initializeApp(this);
|
||||
firebaseAuth = FirebaseAuth.getInstance();
|
||||
|
||||
loginButton.setOnClickListener(v -> validarLogin());
|
||||
criarContaTextView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = new Intent(LoginActivity.this, CriarContaActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
passesquecerTextView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = new Intent(LoginActivity.this, RecupearPasswordActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
googleButton.setOnClickListener(v ->
|
||||
Toast.makeText(this, "Login com Google disponível em breve.", Toast.LENGTH_SHORT).show()
|
||||
);
|
||||
}
|
||||
|
||||
private void validarLogin() {
|
||||
String email = emailEditText.getText().toString().trim();
|
||||
String password = passwordEditText.getText().toString();
|
||||
|
||||
if (!validarEspaços(email, password)) {
|
||||
return;
|
||||
}
|
||||
|
||||
toggleLoading(true);
|
||||
|
||||
firebaseAuth.signInWithEmailAndPassword(email, password)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
toggleLoading(false);
|
||||
if (task.isSuccessful()) {
|
||||
FirebaseUser user = task.getResult().getUser();
|
||||
String welcome = user != null && !TextUtils.isEmpty(user.getEmail())
|
||||
? "Bem-vindo, " + user.getEmail()
|
||||
: "Login realizado com sucesso!";
|
||||
Toast.makeText(this, welcome, Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(this,
|
||||
task.getException() != null ? task.getException().getMessage() : "Falha no login",
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void recupearPassword() {
|
||||
String email = emailEditText.getText().toString().trim();
|
||||
if (TextUtils.isEmpty(email)) {
|
||||
emailEditText.setError("Informe o email para recuperar a palavra-passe.");
|
||||
emailEditText.requestFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
toggleLoading(true);
|
||||
firebaseAuth.sendPasswordResetEmail(email)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
toggleLoading(false);
|
||||
if (task.isSuccessful()) {
|
||||
Toast.makeText(this,
|
||||
"Email de recuperação enviado para " + email,
|
||||
Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(this,
|
||||
task.getException() != null ? task.getException().getMessage() : "Erro ao enviar email",
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean validarEspaços(String email, String password) {
|
||||
if (TextUtils.isEmpty(email)) {
|
||||
emailEditText.setError("Email obrigatório.");
|
||||
emailEditText.requestFocus();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
|
||||
emailEditText.setError("Formato de email inválido.");
|
||||
emailEditText.requestFocus();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(password)) {
|
||||
passwordEditText.setError("Palavra-passe obrigatória.");
|
||||
passwordEditText.requestFocus();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (password.length() < 6) {
|
||||
passwordEditText.setError("Mínimo de 6 caracteres.");
|
||||
passwordEditText.requestFocus();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void toggleLoading(boolean show) {
|
||||
loadingProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
loginButton.setEnabled(!show);
|
||||
googleButton.setEnabled(!show);
|
||||
criarContaTextView.setEnabled(!show);
|
||||
passesquecerTextView.setEnabled(!show);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.lifegrid;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class RecupearPasswordActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
EdgeToEdge.enable(this);
|
||||
setContentView(R.layout.activity_recupear_password);
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
|
||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user