private void accessGithubLoginData(String accessToken, User githubUser){ String provider = "github"; AuthCredential credential = GithubAuthProvider.getCredential(accessToken); credential = provider.equalsIgnoreCase("github") ? GithubAuthProvider.getCredential( accessToken ) : credential; //user.saveProviderSP( LoginActivity.this, provider ); mFirebaseAuth.signInWithCredential(credential) .addOnCompleteListener(this, task -> { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); saveUserGithubData("name", githubUser.name); saveUserGithubData("email", githubUser.email); saveUserGithubData("company", githubUser.company); saveUserGithubData("createdAt", String.valueOf(githubUser.created_at)); saveUserGithubData("bio", githubUser.bio); saveUserGithubData("location", githubUser.location); saveUserGithubData("numOfRepos", String.valueOf(githubUser.public_repos)); saveUserGithubData("followers", String.valueOf(githubUser.followers)); callMainActivity(); if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } }); }
public static AuthCredential getCredential(final JSONObject credential) throws Exception { final String providerId = credential.getString("provider"); AuthCredential authCredential; if (providerId.equals(EmailAuthProvider.PROVIDER_ID)) { authCredential = getEmailAuthCredential(credential); } else if (providerId.equals(FacebookAuthProvider.PROVIDER_ID)) { authCredential = getFacebookAuthCredential(credential); } else if (providerId.equals(GithubAuthProvider.PROVIDER_ID)) { authCredential = getGithubAuthCredential(credential); } else if (providerId.equals(GoogleAuthProvider.PROVIDER_ID)) { authCredential = getGoogleAuthCredential(credential); } else if (providerId.equals(TwitterAuthProvider.PROVIDER_ID)) { authCredential = getTwitterAuthCredential(credential); } else { throw new Exception("Unknown provider ID: " + providerId); } return authCredential; }
private void signInWithToken(String token) { // credential object from the token AuthCredential credential = GithubAuthProvider.getCredential(token); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { writeUserInfo(); } else { Log.d("github signInWithToken", "@3" + task.getException()); } } }); }
private 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] }
/** * https://firebase.google.com/docs/auth/android/github-auth#authenticate_with_firebase */ private void signIn(String token) { if(token == null) { return; } AuthCredential credential = GithubAuthProvider.getCredential(token); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, task -> { Logger.d("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()) { Logger.w("signInWithCredential", task.getException()); Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } }); }
private static AuthCredential getGithubAuthCredential(final JSONObject credential) throws Exception { final String token = credential.getString("token"); return GithubAuthProvider.getCredential(token); }