75 lines
3.1 KiB
Java
75 lines
3.1 KiB
Java
package com.example.pap;
|
|
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.widget.Button;
|
|
import android.widget.Toast;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
public class VerificacaoActivity extends AppCompatActivity {
|
|
|
|
private Button btnJaConfirmei, btnVoltarLoginVerificacao;
|
|
private String emailGuardado, passwordGuardada;
|
|
private SharedPreferences sharedPreferences;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_verificacao);
|
|
|
|
btnJaConfirmei = findViewById(R.id.btnJaConfirmei);
|
|
btnVoltarLoginVerificacao = findViewById(R.id.btnVoltarLoginVerificacao);
|
|
sharedPreferences = getSharedPreferences("MeusDadosApp", MODE_PRIVATE);
|
|
|
|
// Receber o email e a password do ecrã de Registo
|
|
emailGuardado = getIntent().getStringExtra("email_registo");
|
|
passwordGuardada = getIntent().getStringExtra("password_registo");
|
|
|
|
btnJaConfirmei.setOnClickListener(v -> {
|
|
Toast.makeText(this, "A verificar...", Toast.LENGTH_SHORT).show();
|
|
tentarFazerLogin();
|
|
});
|
|
|
|
btnVoltarLoginVerificacao.setOnClickListener(v -> {
|
|
startActivity(new Intent(VerificacaoActivity.this, LoginActivity.class));
|
|
finish();
|
|
});
|
|
}
|
|
|
|
private void tentarFazerLogin() {
|
|
SupabaseApi api = SupabaseConfig.getRetrofit().create(SupabaseApi.class);
|
|
UserCredentials creds = new UserCredentials(emailGuardado, passwordGuardada);
|
|
|
|
api.login(SupabaseConfig.SUPABASE_KEY, creds).enqueue(new Callback<SupabaseResponse>() {
|
|
@Override
|
|
public void onResponse(Call<SupabaseResponse> call, Response<SupabaseResponse> response) {
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
// SUCESSO! O email foi confirmado e o login funcionou
|
|
String token = response.body().access_token;
|
|
|
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
editor.putString("access_token", token);
|
|
editor.putString("email", emailGuardado);
|
|
editor.apply();
|
|
|
|
Toast.makeText(VerificacaoActivity.this, "Email confirmado com sucesso! Bem-vindo!", Toast.LENGTH_LONG).show();
|
|
startActivity(new Intent(VerificacaoActivity.this, HomeActivity.class));
|
|
finish();
|
|
} else {
|
|
// ERRO! Provavelmente o gajo ainda não clicou no link do email
|
|
Toast.makeText(VerificacaoActivity.this, "Ainda não confirmaste! Vai ao teu email e clica no link.", Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Call<SupabaseResponse> call, Throwable t) {
|
|
Toast.makeText(VerificacaoActivity.this, "Sem ligação à internet!", Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
} |