parent
089cb14998
commit
661bbaab46
|
|
@ -27,8 +27,8 @@ android {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_11
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_11
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,20 @@
|
||||||
"package_name": "com.example.lifegrid"
|
"package_name": "com.example.lifegrid"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"oauth_client": [],
|
"oauth_client": [
|
||||||
|
{
|
||||||
|
"client_id": "1019731295596-i3q6aprqj6s55g6s97tpopbk4foutold.apps.googleusercontent.com",
|
||||||
|
"client_type": 1,
|
||||||
|
"android_info": {
|
||||||
|
"package_name": "com.example.lifegrid",
|
||||||
|
"certificate_hash": "d0d27537414e1e5c5f0dd3bf224dbf819e70668b"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"client_id": "1019731295596-llnufhs1t24ijo7afa5c85lkc98pk27q.apps.googleusercontent.com",
|
||||||
|
"client_type": 3
|
||||||
|
}
|
||||||
|
],
|
||||||
"api_key": [
|
"api_key": [
|
||||||
{
|
{
|
||||||
"current_key": "AIzaSyAbQ_-mob81SR07481CSJHP_z3GniS-xdk"
|
"current_key": "AIzaSyAbQ_-mob81SR07481CSJHP_z3GniS-xdk"
|
||||||
|
|
@ -20,7 +33,12 @@
|
||||||
],
|
],
|
||||||
"services": {
|
"services": {
|
||||||
"appinvite_service": {
|
"appinvite_service": {
|
||||||
"other_platform_oauth_client": []
|
"other_platform_oauth_client": [
|
||||||
|
{
|
||||||
|
"client_id": "1019731295596-llnufhs1t24ijo7afa5c85lkc98pk27q.apps.googleusercontent.com",
|
||||||
|
"client_type": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,6 @@ public class CriarContaActivity extends AppCompatActivity {
|
||||||
credentialManager = CredentialManager.create(this);
|
credentialManager = CredentialManager.create(this);
|
||||||
|
|
||||||
loginButton2.setOnClickListener(v -> criarConta());
|
loginButton2.setOnClickListener(v -> criarConta());
|
||||||
googleButton2.setOnClickListener(v -> signInWithGoogle());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void criarConta() {
|
private void criarConta() {
|
||||||
|
|
@ -176,46 +175,6 @@ public class CriarContaActivity extends AppCompatActivity {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void signInWithGoogle() {
|
|
||||||
String webClientId = getString(R.string.default_web_client_id);
|
|
||||||
|
|
||||||
if (webClientId.equals("YOUR_WEB_CLIENT_ID_HERE")) {
|
|
||||||
Toast.makeText(this, "Por favor, configure o Web Client ID no Firebase Console.", Toast.LENGTH_LONG).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
|
|
||||||
.setFilterByAuthorizedAccounts(false)
|
|
||||||
.setServerClientId(webClientId)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
GetCredentialRequest request = new GetCredentialRequest.Builder()
|
|
||||||
.addCredentialOption(googleIdOption)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
toggleLoading(true);
|
|
||||||
|
|
||||||
credentialManager.getCredential(
|
|
||||||
this,
|
|
||||||
request,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleGoogleSignInResult(GetCredentialResponse result) {
|
|
||||||
Credential credential = result.getCredential();
|
|
||||||
|
|
||||||
if (credential instanceof CustomCredential) {
|
|
||||||
CustomCredential customCredential = (CustomCredential) credential;
|
|
||||||
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(customCredential.getType())) {
|
|
||||||
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential
|
|
||||||
.createFrom(customCredential.getData());
|
|
||||||
String idToken = googleIdTokenCredential.getIdToken();
|
|
||||||
firebaseAuthWithGoogle(idToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void firebaseAuthWithGoogle(String idToken) {
|
private void firebaseAuthWithGoogle(String idToken) {
|
||||||
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
||||||
firebaseAuth.signInWithCredential(credential)
|
firebaseAuth.signInWithCredential(credential)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,11 @@ package com.example.lifegrid;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.CancellationSignal;
|
||||||
|
import android.text.Editable;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.text.TextWatcher;
|
||||||
|
import android.util.Log;
|
||||||
import android.util.Patterns;
|
import android.util.Patterns;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
@ -12,17 +16,21 @@ import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.core.graphics.Insets;
|
import androidx.core.graphics.Insets;
|
||||||
import androidx.core.view.ViewCompat;
|
import androidx.core.view.ViewCompat;
|
||||||
import androidx.core.view.WindowInsetsCompat;
|
import androidx.core.view.WindowInsetsCompat;
|
||||||
|
|
||||||
|
import androidx.credentials.ClearCredentialStateRequest;
|
||||||
import androidx.credentials.CredentialManager;
|
import androidx.credentials.CredentialManager;
|
||||||
|
|
||||||
import androidx.credentials.Credential;
|
import androidx.credentials.Credential;
|
||||||
|
import androidx.credentials.CredentialManagerCallback;
|
||||||
import androidx.credentials.CustomCredential;
|
import androidx.credentials.CustomCredential;
|
||||||
import androidx.credentials.GetCredentialRequest;
|
import androidx.credentials.GetCredentialRequest;
|
||||||
import androidx.credentials.GetCredentialResponse;
|
import androidx.credentials.GetCredentialResponse;
|
||||||
|
import androidx.credentials.exceptions.ClearCredentialException;
|
||||||
import androidx.credentials.exceptions.GetCredentialException;
|
import androidx.credentials.exceptions.GetCredentialException;
|
||||||
import androidx.credentials.exceptions.NoCredentialException;
|
import androidx.credentials.exceptions.NoCredentialException;
|
||||||
|
|
||||||
|
|
@ -35,6 +43,8 @@ import com.google.firebase.auth.FirebaseAuth;
|
||||||
import com.google.firebase.auth.FirebaseUser;
|
import com.google.firebase.auth.FirebaseUser;
|
||||||
import com.google.firebase.auth.GoogleAuthProvider;
|
import com.google.firebase.auth.GoogleAuthProvider;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class LoginActivity extends AppCompatActivity {
|
public class LoginActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private TextView textView5;
|
private TextView textView5;
|
||||||
|
|
@ -51,6 +61,11 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
private ProgressBar loadingProgressBar;
|
private ProgressBar loadingProgressBar;
|
||||||
private FirebaseAuth firebaseAuth;
|
private FirebaseAuth firebaseAuth;
|
||||||
private CredentialManager credentialManager;
|
private CredentialManager credentialManager;
|
||||||
|
private FirebaseAuth mAuth;
|
||||||
|
|
||||||
|
private static final String TAG = "LoginActivity - Google Sign In";
|
||||||
|
private static final String GOOGLE_ID_TOKEN_CREDENTIAL = "1019731295596-i3q6aprqj6s55g6s97tpopbk4foutold.apps.googleusercontent.com";
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
@ -76,10 +91,13 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
loadingProgressBar = findViewById(R.id.loadingProgressBar);
|
loadingProgressBar = findViewById(R.id.loadingProgressBar);
|
||||||
|
|
||||||
FirebaseApp.initializeApp(this);
|
FirebaseApp.initializeApp(this);
|
||||||
firebaseAuth = FirebaseAuth.getInstance();
|
mAuth = FirebaseAuth.getInstance();
|
||||||
credentialManager = CredentialManager.create(this);
|
credentialManager = CredentialManager.create(getBaseContext());
|
||||||
|
|
||||||
|
|
||||||
loginButton.setOnClickListener(v -> validarLogin());
|
loginButton.setOnClickListener(v -> validarLogin());
|
||||||
|
googleButton.setOnClickListener(v -> launchCredentialManager());
|
||||||
|
|
||||||
criarContaTextView.setOnClickListener(new View.OnClickListener() {
|
criarContaTextView.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
|
@ -87,21 +105,49 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Inicialmente desabilitar o TextView de recuperar senha
|
||||||
|
passesquecerTextView.setEnabled(false);
|
||||||
|
passesquecerTextView.setAlpha(0.5f); // Visualmente mais claro quando desabilitado
|
||||||
|
|
||||||
passesquecerTextView.setOnClickListener(new View.OnClickListener() {
|
passesquecerTextView.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
String email = emailEditText.getText().toString().trim();
|
String email = emailEditText.getText().toString().trim();
|
||||||
Intent intent = new Intent(LoginActivity.this, RecupearPasswordActivity.class);
|
|
||||||
// Passar o email digitado no login para a atividade de recuperação
|
// Validar se há email antes de abrir a atividade de recuperação
|
||||||
if (!TextUtils.isEmpty(email)) {
|
if (TextUtils.isEmpty(email)) {
|
||||||
intent.putExtra("email", email);
|
emailEditText.setError("Por favor, digite seu email primeiro.");
|
||||||
|
emailEditText.requestFocus();
|
||||||
|
Toast.makeText(LoginActivity.this, "Digite seu email para recuperar a senha.", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(LoginActivity.this, RecupearPasswordActivity.class);
|
||||||
|
intent.putExtra("email", email);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
googleButton.setOnClickListener(v -> signInWithGoogle());
|
|
||||||
|
// Adicionar TextWatcher para habilitar/desabilitar o TextView dinamicamente
|
||||||
|
emailEditText.addTextChangedListener(new TextWatcher() {
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
String email = s.toString().trim();
|
||||||
|
boolean hasEmail = !TextUtils.isEmpty(email);
|
||||||
|
passesquecerTextView.setEnabled(hasEmail);
|
||||||
|
passesquecerTextView.setAlpha(hasEmail ? 1.0f : 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable s) {}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void validarLogin() {
|
private void validarLogin() {
|
||||||
String email = emailEditText.getText().toString().trim();
|
String email = emailEditText.getText().toString().trim();
|
||||||
String password = passwordEditText.getText().toString();
|
String password = passwordEditText.getText().toString();
|
||||||
|
|
@ -187,45 +233,6 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void signInWithGoogle() {
|
|
||||||
String webClientId = getString(R.string.default_web_client_id);
|
|
||||||
|
|
||||||
if (webClientId.equals("YOUR_WEB_CLIENT_ID_HERE")) {
|
|
||||||
Toast.makeText(this, "Por favor, configure o Web Client ID no Firebase Console.", Toast.LENGTH_LONG).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
|
|
||||||
.setFilterByAuthorizedAccounts(false)
|
|
||||||
.setServerClientId(webClientId)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
GetCredentialRequest request = new GetCredentialRequest.Builder()
|
|
||||||
.addCredentialOption(googleIdOption)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
toggleLoading(true);
|
|
||||||
|
|
||||||
credentialManager.getCredential(
|
|
||||||
this,
|
|
||||||
request,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleGoogleSignInResult(GetCredentialResponse result) {
|
|
||||||
Credential credential = result.getCredential();
|
|
||||||
|
|
||||||
if (credential instanceof CustomCredential) {
|
|
||||||
CustomCredential customCredential = (CustomCredential) credential;
|
|
||||||
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(customCredential.getType())) {
|
|
||||||
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential
|
|
||||||
.createFrom(customCredential.getData());
|
|
||||||
String idToken = googleIdTokenCredential.getIdToken();
|
|
||||||
firebaseAuthWithGoogle(idToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void firebaseAuthWithGoogle(String idToken) {
|
private void firebaseAuthWithGoogle(String idToken) {
|
||||||
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
||||||
|
|
@ -259,4 +266,84 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
criarContaTextView.setEnabled(!show);
|
criarContaTextView.setEnabled(!show);
|
||||||
passesquecerTextView.setEnabled(!show);
|
passesquecerTextView.setEnabled(!show);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void launchCredentialManager() {
|
||||||
|
// [START create_credential_manager_request]
|
||||||
|
// Instantiate a Google sign-in request
|
||||||
|
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
|
||||||
|
.setFilterByAuthorizedAccounts(true)
|
||||||
|
.setServerClientId(getString(R.string.default_web_client_id))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Create the Credential Manager request
|
||||||
|
GetCredentialRequest request = new GetCredentialRequest.Builder()
|
||||||
|
.addCredentialOption(googleIdOption)
|
||||||
|
.build();
|
||||||
|
// [END create_credential_manager_request]
|
||||||
|
|
||||||
|
// Launch Credential Manager UI
|
||||||
|
credentialManager.getCredentialAsync(
|
||||||
|
getBaseContext(),
|
||||||
|
request,
|
||||||
|
new CancellationSignal(),
|
||||||
|
Executors.newSingleThreadExecutor(),
|
||||||
|
new CredentialManagerCallback<>() {
|
||||||
|
@Override
|
||||||
|
public void onResult(GetCredentialResponse result) {
|
||||||
|
// Extract credential from the result returned by Credential Manager
|
||||||
|
handleSignIn(result.getCredential());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(GetCredentialException e) {
|
||||||
|
Log.e(TAG, "Couldn't retrieve user's credentials: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleSignIn(Credential credential) {
|
||||||
|
// Check if credential is of type Google ID
|
||||||
|
if (credential instanceof CustomCredential customCredential
|
||||||
|
&& credential.getType().equals(GOOGLE_ID_TOKEN_CREDENTIAL)) {
|
||||||
|
// Create Google ID Token
|
||||||
|
Bundle credentialData = customCredential.getData();
|
||||||
|
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credentialData);
|
||||||
|
|
||||||
|
// Sign in to Firebase with using the token
|
||||||
|
firebaseAuthWithGoogle(googleIdTokenCredential.getIdToken());
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "Credential is not of type Google ID!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void signOut() {
|
||||||
|
// Firebase sign out
|
||||||
|
mAuth.signOut();
|
||||||
|
|
||||||
|
// When a user signs out, clear the current user credential state from all credential providers.
|
||||||
|
ClearCredentialStateRequest clearRequest = new ClearCredentialStateRequest();
|
||||||
|
credentialManager.clearCredentialStateAsync(
|
||||||
|
clearRequest,
|
||||||
|
new CancellationSignal(),
|
||||||
|
Executors.newSingleThreadExecutor(),
|
||||||
|
new CredentialManagerCallback<>() {
|
||||||
|
@Override
|
||||||
|
public void onResult(@NonNull Void result) {
|
||||||
|
//updateUI(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(@NonNull ClearCredentialException e) {
|
||||||
|
Log.e(TAG, "Couldn't clear user credentials: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
// Check if user is signed in (non-null) and update UI accordingly.
|
||||||
|
FirebaseUser currentUser = mAuth.getCurrentUser();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">LifeGrid</string>
|
<string name="app_name">LifeGrid</string>
|
||||||
<!-- Substitua pelo Web Client ID do Firebase Console -> Authentication -> Sign-in method -> Google -->
|
<!-- Substitua pelo Web Client ID do Firebase Console -> Authentication -> Sign-in method -> Google -->
|
||||||
<string name="default_web_client_id">1019731295596-llnufhs1t24ijo7afa5c85lkc98pk27q.apps.googleusercontent.com</string>
|
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in New Issue