下面列出了io.reactivex.annotations.CheckReturnValue#com.google.firebase.auth.AuthResult 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
});
}
public void before(Context context) throws Exception {
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setApplicationId("1:1010498001935:android:f17a2f247ad8e8bc")
.setApiKey("AIzaSyBys-YxxE7kON5PxZc5aY6JwVvreyx_owc")
.setDatabaseUrl(databaseUrl)
.build();
FirebaseApp.initializeApp(context, firebaseOptions);
FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG);
}
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
Task<AuthResult> signInTask = TaskUtils.waitForTask(FirebaseAuth.getInstance().signInAnonymously());
if (signInTask.isSuccessful()) {
Log.d(TAG, "Signed in as " + signInTask.getResult().getUser());
} else {
throw new Exception("Failed to sign in: " + signInTask.getException());
}
}
this.databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(databaseUrl);
}
@Test
public void linkWithCredential() {
TestSubscriber<AuthResult> testSubscriber = new TestSubscriber<>();
RxFirebaseUser.linkWithCredential(mockUser, credential)
.subscribeOn(Schedulers.immediate())
.subscribe(testSubscriber);
testOnSuccessListener.getValue().onSuccess(authResult);
testOnCompleteListener.getValue().onComplete(mockAuthResultTask);
verify(mockUser).linkWithCredential(credential);
testSubscriber.assertNoErrors();
testSubscriber.assertValueCount(1);
testSubscriber.assertReceivedOnNext(Collections.singletonList(authResult));
testSubscriber.assertCompleted();
testSubscriber.unsubscribe();
}
private void handleAnonymousUpgradeLinkingFlow(final HelperActivityBase activity,
final OAuthProvider provider,
final FlowParameters flowParameters) {
AuthOperationManager.getInstance().safeGenericIdpSignIn(activity, provider, flowParameters)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Pass the credential so we can sign-in on the after the merge
// conflict is resolved.
handleSuccess(provider.getProviderId(),
authResult.getUser(), (OAuthCredential) authResult.getCredential(),
/* setPendingCredential= */true);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
setResult(Resource.<IdpResponse>forFailure(e));
}
});
}
private void loginWithFirebaseEmailPassword(String email, String password) {
mHelpers.showProgressDialog(getString(R.string.loading_msg));
mFirebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
mHelpers.hideProgressDialog();
if (task.isSuccessful()) {
mHelpers.showToast(getString(R.string.login_success));
onSignInSuccess();
} else {
Log.w("Sign in failed", task.getException().toString());
mHelpers.showAlertDialog(getString(R.string.error_message), task.getException().getMessage()).show();
}
}
});
}
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();
}
}
});
}
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();
}
}
});
}
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();
}
}
});
}
private void registerUser(final String inputName, final String inputPw, String inputEmail) {
progressDialog.setMessage("Verificating...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(inputEmail,inputPw).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
progressDialog.dismiss();
sendUserData(inputName, inputPw);
Toast.makeText(RegistrationActivity.this,"You've been registered successfully.",Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegistrationActivity.this,MainActivity.class));
}
else{
progressDialog.dismiss();
Toast.makeText(RegistrationActivity.this,"Email already exists.",Toast.LENGTH_SHORT).show();
}
}
});
}
public void signUser(String email, String password){
progressDialog.setMessage("Verificating...");
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Login Successful",Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}
else{
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Invalid email or password",Toast.LENGTH_SHORT).show();
}
}
});
}
@ReactMethod
public void signInAnonymously(final Callback callback) {
mAuth = FirebaseAuth.getInstance();
mAuth.signInAnonymously()
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful());
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);
}
});
}
@Test
public void signInWithCustomToken() {
TestSubscriber<AuthResult> testSubscriber = new TestSubscriber<>();
RxFirebaseAuth.signInWithCustomToken(mockAuth, "token")
.subscribeOn(Schedulers.immediate())
.subscribe(testSubscriber);
testOnSuccessListener.getValue().onSuccess(mockAuthResult);
testOnCompleteListener.getValue().onComplete(mockAuthTask);
verify(mockAuth).signInWithCustomToken(eq("token"));
testSubscriber.assertNoErrors();
testSubscriber.assertValueCount(1);
testSubscriber.assertReceivedOnNext(Collections.singletonList(mockAuthResult));
testSubscriber.assertCompleted();
testSubscriber.unsubscribe();
}
private void createUserOnFirebaseAuthentication(final String email,
final String password,
final OnUserCreatedOnFirebaseCallback onUserCreatedOnFirebaseCallback) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(ChatSignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// Toast.makeText(ChatSignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// 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()) {
onUserCreatedOnFirebaseCallback.onUserCreatedError(task.getException());
} else {
// user created with success
FirebaseUser firebaseUser = mAuth.getCurrentUser(); // retrieve the created user
onUserCreatedOnFirebaseCallback.onUserCreatedSuccess(firebaseUser.getUid());
}
}
});
}
public void doRegister(String uname,String pwd){
Log.e(TAG,uname+" "+pwd);
mAuth.createUserWithEmailAndPassword(uname, pwd)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail: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()) {
Toast.makeText(getApplicationContext(), "Registration failed",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "You have successfully Registered",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
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);
}
});
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.explore_button:
mAuth.signInAnonymously().addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Intent feedsIntent = new Intent(WelcomeActivity.this, FeedsActivity.class);
startActivity(feedsIntent);
}
}).addOnFailureListener( new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(WelcomeActivity.this, "Unable to sign in anonymously.",
Toast.LENGTH_SHORT).show();
Log.e(TAG, e.getMessage());
}
});
break;
case R.id.sign_in_button:
Intent signInIntent = new Intent(this, ProfileActivity.class);
startActivity(signInIntent);
break;
}
}
@ReactMethod
public void signInWithEmail(final String email, final String password, final Callback callback) {
mAuth = FirebaseAuth.getInstance();
mAuth.signInWithEmailAndPassword(email, password)
.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) {
Log.e(TAG, "An exception occurred: " + ex.getMessage());
userExceptionCallback(ex, callback);
}
});
}
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();
}
}
});
}
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();
}
});
}
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();
}
});
}
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();
}
// ...
}
});
}
@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);
}
});
}
@Override
public void performFirebaseLogin(final Activity activity, final String email, String password) {
FirebaseAuth.getInstance()
.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "performFirebaseLogin: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()) {
mOnLoginListener.onSuccess(task.getResult().toString());
updateFirebaseToken(task.getResult().getUser().getUid(),
new SharedPrefUtil(activity.getApplicationContext()).getString(Constants.ARG_FIREBASE_TOKEN, null));
} else {
mOnLoginListener.onFailure(task.getException().getMessage());
}
}
});
}
@Override
public void performFirebaseLogin(final Activity activity, final String email, String password) {
FirebaseAuth.getInstance()
.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "performFirebaseLogin: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()) {
mOnLoginListener.onSuccess(task.getResult().toString());
updateFirebaseToken(task.getResult().getUser().getUid(),
new SharedPrefUtil(activity.getApplicationContext()).getString(Constants.ARG_FIREBASE_TOKEN, null));
} else {
mOnLoginListener.onFailure(task.getException().getMessage());
}
}
});
}
@Override
public void performFirebaseRegistration(Activity activity, final String email, String password) {
FirebaseAuth.getInstance()
.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.e(TAG, "performFirebaseRegistration: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()) {
mOnRegistrationListener.onFailure(task.getException().getMessage());
} else {
// Add the user to users table.
/*DatabaseReference database= FirebaseDatabase.getInstance().getReference();
User user = new User(task.getResult().getUser().getUid(), email);
database.child("users").push().setValue(user);*/
mOnRegistrationListener.onSuccess(task.getResult().getUser());
}
}
});
}
@Test
public void signInAnonymously_Failed() {
TestSubscriber<AuthResult> testSubscriber = new TestSubscriber<>();
RxFirebaseAuth.signInAnonymously(mockAuth)
.subscribeOn(Schedulers.immediate())
.subscribe(testSubscriber);
Exception e = new Exception("something bad happened");
testOnFailureListener.getValue().onFailure(e);
verify(mockAuth).signInAnonymously();
testSubscriber.assertError(e);
testSubscriber.assertNotCompleted();
testSubscriber.unsubscribe();
}
@Test
public void signInWithEmailAndPassword() {
TestSubscriber<AuthResult> testSubscriber = new TestSubscriber<>();
RxFirebaseAuth.signInWithEmailAndPassword(mockAuth, "email", "password")
.subscribeOn(Schedulers.immediate())
.subscribe(testSubscriber);
testOnSuccessListener.getValue().onSuccess(mockAuthResult);
testOnCompleteListener.getValue().onComplete(mockAuthTask);
verify(mockAuth).signInWithEmailAndPassword(eq("email"), eq("password"));
testSubscriber.assertNoErrors();
testSubscriber.assertValueCount(1);
testSubscriber.assertReceivedOnNext(Collections.singletonList(mockAuthResult));
testSubscriber.assertCompleted();
testSubscriber.unsubscribe();
}
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]
}
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]
}
public void unlink(String providerId) {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// [START auth_unlink]
mAuth.getCurrentUser().unlink(providerId)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Auth provider unlinked from account
// ...
}
}
});
// [END auth_unlink]
}