first commit
This commit is contained in:
93
app/src/main/java/com/example/pap/LoginActivity.java
Normal file
93
app/src/main/java/com/example/pap/LoginActivity.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package com.example.pap;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
private EditText etLoginEmail, etLoginPassword;
|
||||
private Button btnLogin;
|
||||
private TextView tvGoToRegister;
|
||||
private SupabaseApi api;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
|
||||
// 1. Inicializar os componentes
|
||||
etLoginEmail = findViewById(R.id.etLoginEmail);
|
||||
etLoginPassword = findViewById(R.id.etLoginPassword);
|
||||
btnLogin = findViewById(R.id.btnLogin);
|
||||
tvGoToRegister = findViewById(R.id.tvGoToRegister);
|
||||
|
||||
// 2. Iniciar a comunicação com a API do Supabase
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl(SupabaseConfig.URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
api = retrofit.create(SupabaseApi.class);
|
||||
|
||||
// 3. Configurar os cliques
|
||||
btnLogin.setOnClickListener(v -> efetuarLogin());
|
||||
|
||||
tvGoToRegister.setOnClickListener(v -> {
|
||||
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
|
||||
startActivity(intent);
|
||||
});
|
||||
}
|
||||
|
||||
private void efetuarLogin() {
|
||||
String email = etLoginEmail.getText().toString().trim();
|
||||
String password = etLoginPassword.getText().toString().trim();
|
||||
|
||||
// Validação básica
|
||||
if (email.isEmpty() || password.isEmpty()) {
|
||||
Toast.makeText(this, "Preencha o email e a password!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
UserCredentials credentials = new UserCredentials(email, password);
|
||||
|
||||
// Faz o pedido de Login ao Supabase
|
||||
api.login(SupabaseConfig.API_KEY, credentials).enqueue(new Callback<SupabaseResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupabaseResponse> call, Response<SupabaseResponse> response) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
|
||||
// Login com sucesso! Vai para a Home
|
||||
Toast.makeText(LoginActivity.this, "Login com sucesso!", Toast.LENGTH_SHORT).show();
|
||||
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
|
||||
startActivity(intent);
|
||||
finish(); // Fecha a tela de login
|
||||
|
||||
} else {
|
||||
// SE DER ERRO (ex: 404, 400), MOSTRA O LINK EXATO E O CÓDIGO
|
||||
String urlQueFalhou = response.raw().request().url().toString();
|
||||
if (response.code() == 400) {
|
||||
Toast.makeText(LoginActivity.this, "Email ou password incorretos!", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(LoginActivity.this, "Erro " + response.code() + "! Link: " + urlQueFalhou, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<SupabaseResponse> call, Throwable t) {
|
||||
Toast.makeText(LoginActivity.this, "Falha na ligação: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user