recuperação de passe e uns erritos no google
parent
7aa8b79009
commit
089cb14998
|
|
@ -44,7 +44,6 @@ dependencies {
|
|||
implementation(libs.googleid)
|
||||
implementation(libs.firebase.database)
|
||||
implementation(libs.recyclerview)
|
||||
implementation(libs.play.services.auth)
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.ext.junit)
|
||||
androidTestImplementation(libs.espresso.core)
|
||||
|
|
|
|||
|
|
@ -11,19 +11,22 @@ import android.widget.ProgressBar;
|
|||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
||||
import com.google.android.gms.common.api.ApiException;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import androidx.credentials.Credential;
|
||||
import androidx.credentials.CustomCredential;
|
||||
import androidx.credentials.GetCredentialRequest;
|
||||
import androidx.credentials.GetCredentialResponse;
|
||||
import androidx.credentials.exceptions.GetCredentialException;
|
||||
import androidx.credentials.exceptions.NoCredentialException;
|
||||
import androidx.credentials.CredentialManager;
|
||||
|
||||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.auth.AuthCredential;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
|
|
@ -40,7 +43,7 @@ public class CriarContaActivity extends AppCompatActivity {
|
|||
private Button googleButton2;
|
||||
private ProgressBar loadingProgressBar;
|
||||
private FirebaseAuth firebaseAuth;
|
||||
private GoogleSignInClient googleSignInClient;
|
||||
private CredentialManager credentialManager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
|
@ -63,13 +66,7 @@ public class CriarContaActivity extends AppCompatActivity {
|
|||
|
||||
FirebaseApp.initializeApp(this);
|
||||
firebaseAuth = FirebaseAuth.getInstance();
|
||||
|
||||
// Configurar Google Sign-In
|
||||
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
.requestIdToken(getString(R.string.default_web_client_id))
|
||||
.requestEmail()
|
||||
.build();
|
||||
googleSignInClient = GoogleSignIn.getClient(this, gso);
|
||||
credentialManager = CredentialManager.create(this);
|
||||
|
||||
loginButton2.setOnClickListener(v -> criarConta());
|
||||
googleButton2.setOnClickListener(v -> signInWithGoogle());
|
||||
|
|
@ -180,29 +177,46 @@ public class CriarContaActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private void signInWithGoogle() {
|
||||
Intent signInIntent = googleSignInClient.getSignInIntent();
|
||||
googleSignInLauncher.launch(signInIntent);
|
||||
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;
|
||||
}
|
||||
|
||||
private final ActivityResultLauncher<Intent> googleSignInLauncher = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
if (result.getResultCode() == RESULT_OK) {
|
||||
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
|
||||
try {
|
||||
GoogleSignInAccount account = task.getResult(ApiException.class);
|
||||
firebaseAuthWithGoogle(account.getIdToken());
|
||||
} catch (ApiException e) {
|
||||
Toast.makeText(this, "Erro ao criar conta com Google: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(this, "Criação de conta com Google cancelada.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
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) {
|
||||
toggleLoading(true);
|
||||
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
||||
firebaseAuth.signInWithCredential(credential)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
|
|
|
|||
|
|
@ -12,19 +12,23 @@ import android.widget.TextView;
|
|||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
||||
import com.google.android.gms.common.api.ApiException;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import androidx.credentials.CredentialManager;
|
||||
|
||||
import androidx.credentials.Credential;
|
||||
import androidx.credentials.CustomCredential;
|
||||
import androidx.credentials.GetCredentialRequest;
|
||||
import androidx.credentials.GetCredentialResponse;
|
||||
import androidx.credentials.exceptions.GetCredentialException;
|
||||
import androidx.credentials.exceptions.NoCredentialException;
|
||||
|
||||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.auth.AuthCredential;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
|
|
@ -46,8 +50,7 @@ public class LoginActivity extends AppCompatActivity {
|
|||
private TextView ouTextView;
|
||||
private ProgressBar loadingProgressBar;
|
||||
private FirebaseAuth firebaseAuth;
|
||||
private GoogleSignInClient googleSignInClient;
|
||||
private static final int RC_SIGN_IN = 9001;
|
||||
private CredentialManager credentialManager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
|
@ -74,13 +77,7 @@ public class LoginActivity extends AppCompatActivity {
|
|||
|
||||
FirebaseApp.initializeApp(this);
|
||||
firebaseAuth = FirebaseAuth.getInstance();
|
||||
|
||||
// Configurar Google Sign-In
|
||||
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
.requestIdToken(getString(R.string.default_web_client_id))
|
||||
.requestEmail()
|
||||
.build();
|
||||
googleSignInClient = GoogleSignIn.getClient(this, gso);
|
||||
credentialManager = CredentialManager.create(this);
|
||||
|
||||
loginButton.setOnClickListener(v -> validarLogin());
|
||||
criarContaTextView.setOnClickListener(new View.OnClickListener() {
|
||||
|
|
@ -191,29 +188,46 @@ public class LoginActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
private void signInWithGoogle() {
|
||||
Intent signInIntent = googleSignInClient.getSignInIntent();
|
||||
googleSignInLauncher.launch(signInIntent);
|
||||
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;
|
||||
}
|
||||
|
||||
private final ActivityResultLauncher<Intent> googleSignInLauncher = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
if (result.getResultCode() == RESULT_OK) {
|
||||
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
|
||||
try {
|
||||
GoogleSignInAccount account = task.getResult(ApiException.class);
|
||||
firebaseAuthWithGoogle(account.getIdToken());
|
||||
} catch (ApiException e) {
|
||||
Toast.makeText(this, "Erro ao fazer login com Google: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(this, "Login com Google cancelado.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
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) {
|
||||
toggleLoading(true);
|
||||
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
|
||||
firebaseAuth.signInWithCredential(credential)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<resources>
|
||||
<string name="app_name">LifeGrid</string>
|
||||
<!-- Substitua pelo Web Client ID do Firebase Console -> Authentication -> Sign-in method -> Google -->
|
||||
<string name="default_web_client_id">YOUR_WEB_CLIENT_ID_HERE</string>
|
||||
<string name="default_web_client_id">1019731295596-llnufhs1t24ijo7afa5c85lkc98pk27q.apps.googleusercontent.com</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue