com.google.firebase.auth.AuthCredential Java Examples

The following examples show how to use com.google.firebase.auth.AuthCredential. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleProviderHandler.java    From capacitor-firebase-auth with MIT License 7 votes vote down vote up
@Override
public void fillResult(AuthCredential credential, JSObject jsResult) {
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this.plugin.getContext());
    if (account != null)  {
        jsResult.put("idToken", account.getIdToken());
    } else {
        Log.w(GOOGLE_TAG, "Ops, there was not last signed in account on google api.");
    }
}
 
Example #2
Source File: FirestackAuth.java    From react-native-firestack with MIT License 6 votes vote down vote up
@ReactMethod
public void googleLogin(String IdToken, final Callback callback) {
    mAuth = FirebaseAuth.getInstance();

    AuthCredential credential = GoogleAuthProvider.getCredential(IdToken, null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful()) {
                      FirestackAuthModule.this.user = task.getResult().getUser();
                      userCallback(FirestackAuthModule.this.user, callback);
                  }else{
                      // userErrorCallback(task, callback);
                  }
                }
            }).addOnFailureListener(new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception ex) {
            userExceptionCallback(ex, callback);
          }
        });
}
 
Example #3
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void linkAndMerge(AuthCredential credential) {
    FirebaseAuth mAuth = FirebaseAuth.getInstance();

    // [START auth_link_and_merge]
    FirebaseUser prevUser = FirebaseAuth.getInstance().getCurrentUser();
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    FirebaseUser currentUser = task.getResult().getUser();
                    // Merge prevUser and currentUser accounts and data
                    // ...
                }
            });
    // [END auth_link_and_merge]
}
 
Example #4
Source File: SigninActivity.java    From wmn-safety with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(final GoogleSignInAccount account) {
    AuthCredential mCredential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(mCredential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        String email = account.getEmail();
                        String name = account.getDisplayName();
                        String userID = mFirebaseAuth.getCurrentUser().getUid();
                        boolean newUser = task.getResult().getAdditionalUserInfo().isNewUser();
                        addNewUser(email, name, "", userID, newUser);
                    } else {
                        Log.w(TAG, task.getException().toString());
                        Toast.makeText(SigninActivity.this, task.getException().toString(), Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            });
}
 
Example #5
Source File: LoginPresenter.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void handleGoogleSignInResult(GoogleSignInResult result) {
    ifViewAttached(view -> {
        if (result.isSuccess()) {
            view.showProgress();

            GoogleSignInAccount account = result.getSignInAccount();

            view.setProfilePhotoUrl(buildGooglePhotoUrl(account.getPhotoUrl()));

            AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
            view.firebaseAuthWithCredentials(credential);

            LogUtil.logDebug(TAG, "firebaseAuthWithGoogle:" + account.getId());

        } else {
            LogUtil.logDebug(TAG, "SIGN_IN_GOOGLE failed :" + result);
            view.hideProgress();
        }
    });
}
 
Example #6
Source File: FirestackAuth.java    From react-native-firestack with MIT License 6 votes vote down vote up
@ReactMethod
public void facebookLogin(String Token, final Callback callback) {
    mAuth = FirebaseAuth.getInstance();

    AuthCredential credential = FacebookAuthProvider.getCredential(Token);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful()) {
                      FirestackAuthModule.this.user = task.getResult().getUser();
                      userCallback(FirestackAuthModule.this.user, callback);
                  }else{
                      // userErrorCallback(task, callback);
                  }
                }
            }).addOnFailureListener(new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception ex) {
            userExceptionCallback(ex, callback);
          }
        });
}
 
Example #7
Source File: FireSignin.java    From Learning-Resources with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        mPrgrsbrMain.setVisibility(View.VISIBLE);

        LogManager.printLog(LOGTYPE_INFO, "signInWithGoogleId: " + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mFireAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        LogManager.printLog(LOGTYPE_DEBUG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        if (!task.isSuccessful()) {

                            mPrgrsbrMain.setVisibility(View.GONE);
                            Log.w(LOG_TAG, "signInWithCredential", task.getException());
                            Snackbar.make(mCrdntrlyot, "Authentication failed.\n" + task.getException().getMessage(),
                                    Snackbar.LENGTH_LONG).show();
                        } else
                            successLoginGetData(task);
                    }
                });
    }
 
Example #8
Source File: LoginActivity.java    From FaceT with Mozilla Public License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    final AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(LoginActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        mProgress.dismiss();
                        Snackbar snackbar = Snackbar.make(activity_login_layout, "Login successfully", Snackbar.LENGTH_SHORT);
                        snackbar.show();
                        checkUserExist();
                    }
                }
            });
}
 
Example #9
Source File: PhoneProviderHandler.java    From capacitor-firebase-auth with MIT License 6 votes vote down vote up
@Override
public void signIn(PluginCall call) {
    if (!call.getData().has("data")) {
        call.reject("The auth data is required");
        return;
    }

    JSObject data = call.getObject("data", new JSObject());

    String phone = data.getString("phone", "");
    if (phone.equalsIgnoreCase("null") || phone.equalsIgnoreCase("")) {
        call.reject("The phone number is required");
        return;
    }

    String code = data.getString("verificationCode", "");
    if(code.equalsIgnoreCase("null") || code.equalsIgnoreCase("")) {
        PhoneAuthProvider.getInstance().verifyPhoneNumber
                (phone, 60, TimeUnit.SECONDS, this.plugin.getActivity(), this.mCallbacks);
    } else {
        AuthCredential credential = PhoneAuthProvider.getCredential(this.mVerificationId, code);
        this.mVerificationCode = code;
        plugin.handleAuthCredentials(credential);
    }
}
 
Example #10
Source File: Loginactivity.java    From LuxVilla with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Snackbar.make(linearLayout,"Ocorreu um erro ao conectar", Snackbar.LENGTH_LONG).show();
                    } else {
                        startActivity(new Intent(Loginactivity.this, MainActivity.class));
                        finish();
                    }
                }
            });
}
 
Example #11
Source File: CapacitorFirebaseAuth.java    From capacitor-firebase-auth with MIT License 6 votes vote down vote up
public void handleAuthCredentials(AuthCredential credential) {
    final PluginCall savedCall = getSavedCall();
    if (savedCall == null) {
        Log.d(PLUGIN_TAG, "No saved call on activity result.");
        return;
    }

    if (credential == null) {
        Log.w(PLUGIN_TAG, "Sign In failure: credentials.");
        savedCall.reject("Sign In failure: credentials.");
        return;
    }

    if (this.nativeAuth) {
        nativeAuth(savedCall, credential);
    } else {
        JSObject jsResult = this.build(credential, savedCall);
        savedCall.success(jsResult);
    }
}
 
Example #12
Source File: FireSignin.java    From Learning-Resources with MIT License 6 votes vote down vote up
private void firebaseAuthWithFacebook(AccessToken accessToken) {

        mPrgrsbrMain.setVisibility(View.VISIBLE);

        LogManager.printLog(LOGTYPE_INFO, "signInWithFacebookToken: " + accessToken);

        AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
        mFireAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        LogManager.printLog(LOGTYPE_DEBUG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        if (!task.isSuccessful()) {

                            mPrgrsbrMain.setVisibility(View.GONE);
                            Log.w(LOG_TAG, "signInWithCredential", task.getException());
                            Snackbar.make(mCrdntrlyot, "Authentication failed.\n" + task.getException().getMessage(),
                                    Snackbar.LENGTH_LONG).show();
                        } else
                            successLoginGetData(task);
                    }
                });
    }
 
Example #13
Source File: SignInActivity.java    From jterm-cswithandroid with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(SignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        startActivity(new Intent(SignInActivity.this, MainActivity.class));
                        finish();
                    }
                }
            });
}
 
Example #14
Source File: LoginActivity.java    From NaviBee with GNU General Public License v3.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
//        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
//                            Log.d(TAG, "signInWithCredential:success");
                            checkSignIn();
                        } else {
                            // If sign in fails, display a message to the user.
                            Toast.makeText(LoginActivity.this, "Sign in fails", Toast.LENGTH_LONG).show();
                        }

                    }
                });
    }
 
Example #15
Source File: SignInActivity.java    From budgetto with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        loginError("Firebase auth failed.");
                        hideProgressView();
                    }
                }
            });
}
 
Example #16
Source File: SignInActivity.java    From Duolingo-Clone with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            //take me to main page
                            Toast.makeText(context, "itworked", Toast.LENGTH_SHORT).show();

                            ActivityNavigation.getInstance(SignInActivity.this).takeToRandomTask();

                        } else {

                            Toast.makeText(context, getString(R.string.auth_failed), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
 
Example #17
Source File: LoginActivity.java    From triviums with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    binding.signInButton.setVisibility(View.GONE);
    binding.progressBar.setVisibility(View.VISIBLE);

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, task -> {
                if (task.isSuccessful()) {
                    viewModel.setLoggedIn(true);
                    // Sign in success, update UI with the signed-in user's information
                    binding.progressBar.setVisibility(View.GONE);
                } else {
                    // If sign in fails, display a message to the user.
                    Toast.makeText(LoginActivity.this, "Authentication failed.",
                            Toast.LENGTH_SHORT).show();
                }

            });
}
 
Example #18
Source File: ProfileActivity.java    From friendlypix-android with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    showProgressDialog(getString(R.string.profile_progress_message));
    mAuth.signInWithCredential(credential)
            .addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult result) {
                    handleFirebaseAuthResult(result);
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    FirebaseCrash.logcat(Log.ERROR, TAG, "auth:onFailure:" + e.getMessage());
                    handleFirebaseAuthResult(null);
                }
            });
}
 
Example #19
Source File: LoginActivity.java    From android-paypal-example with Apache License 2.0 6 votes vote down vote up
private void handleFacebookAccessToken(AccessToken token) {

        Log.d(TAG, "handleFacebookAccessToken:" + token);

        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            updateUI(mAuth.getCurrentUser());
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }
 
Example #20
Source File: SignInActivity.java    From codelab-friendlychat-android with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(SignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        startActivity(new Intent(SignInActivity.this, MainActivity.class));
                        finish();
                    }
                }
            });
}
 
Example #21
Source File: SignInActivity.java    From codelab-friendlychat-android with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(SignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        startActivity(new Intent(SignInActivity.this, MainActivity.class));
                        finish();
                    }
                }
            });
}
 
Example #22
Source File: SignInBaseActivity.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    MyLg.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()+" "+acct.getPhotoUrl());
    showProgressDialog();

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    MyLg.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        MyLg.w(TAG, "signInWithCredential");
                        Toas.show(context, "Authentication failed.");
                    }
                    hideProgressDialog();
                }
            });
}
 
Example #23
Source File: SaveReports.java    From Crimson with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        showProgressDialog();

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(SaveReports.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        hideProgressDialog();
                    }
                });
    }
 
Example #24
Source File: BaseAuthActivity.java    From MangoBloggerAndroidApp with Mozilla Public License 2.0 6 votes vote down vote up
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            hideProgressDialog();
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
//                            FirebaseUser user = mAuth.getCurrentUser();
                            startApp();
                        } else {
                            hideProgressDialog();
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(BaseAuthActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                        // ...
                    }
                });
    }
 
Example #25
Source File: LoginPresenter.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void handleGoogleSignInResult(GoogleSignInResult result) {
    ifViewAttached(view -> {
        if (result.isSuccess()) {
            view.showProgress();

            GoogleSignInAccount account = result.getSignInAccount();

            view.setProfilePhotoUrl(buildGooglePhotoUrl(account.getPhotoUrl()));

            AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
            view.firebaseAuthWithCredentials(credential);

            LogUtil.logDebug(TAG, "firebaseAuthWithGoogle:" + account.getId());

        } else {
            LogUtil.logDebug(TAG, "SIGN_IN_GOOGLE failed :" + result);
            view.hideProgress();
        }
    });
}
 
Example #26
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithPlayGames:" + acct.getId());

    final FirebaseAuth auth = FirebaseAuth.getInstance();
    AuthCredential credential = PlayGamesAuthProvider.getCredential(acct.getServerAuthCode());
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = auth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }

                    // ...
                }
            });
}
 
Example #27
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void reauthWithLink(String email, String emailLink) {
    FirebaseAuth auth = FirebaseAuth.getInstance();

    // [START auth_reauth_with_link]
    // Construct the email link credential from the current URL.
    AuthCredential credential =
            EmailAuthProvider.getCredentialWithLink(email, emailLink);

    // Re-authenticate the user with this credential.
    auth.getCurrentUser().reauthenticateAndRetrieveData(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // User is now successfully reauthenticated
                    } else {
                        Log.e(TAG, "Error reauthenticating", task.getException());
                    }
                }
            });
    // [END auth_reauth_with_link]
}
 
Example #28
Source File: ReferralActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void rewardUser(AuthCredential credential) {
    // [START ddl_referral_reward_user]
    FirebaseAuth.getInstance().getCurrentUser()
            .linkWithCredential(credential)
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    // Complete any post sign-up tasks here.

                    // Trigger the sign-up reward function by creating the
                    // "last_signin_at" field. (If this is a value you want to track,
                    // you would also update this field in the success listeners of
                    // your Firebase Authentication signIn calls.)
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    DatabaseReference userRecord =
                            FirebaseDatabase.getInstance().getReference()
                                    .child("users")
                                    .child(user.getUid());
                    userRecord.child("last_signin_at").setValue(ServerValue.TIMESTAMP);
                }
            });
    // [END ddl_referral_reward_user]
}
 
Example #29
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void reauthenticate() {
    // [START reauthenticate]
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    // Get auth credentials from the user for re-authentication. The example below shows
    // email and password credentials but there are multiple possible providers,
    // such as GoogleAuthProvider or FacebookAuthProvider.
    AuthCredential credential = EmailAuthProvider
            .getCredential("[email protected]", "password1234");

    // Prompt the user to re-provide their sign-in credentials
    user.reauthenticate(credential)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Log.d(TAG, "User re-authenticated.");
                }
            });
    // [END reauthenticate]
}
 
Example #30
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void authWithGithub() {
    FirebaseAuth mAuth = FirebaseAuth.getInstance();

    // [START auth_with_github]
    String token = "<GITHUB-ACCESS-TOKEN>";
    AuthCredential credential = GithubAuthProvider.getCredential(token);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

                    // ...
                }
            });
    // [END auth_with_github]
}