I am working with a "Log In" activity:
LogInActivty extends AppCompatActivity
implements LoaderCallbacks<Cursor>, Firebase.AuthResultHandler, Firebase.AuthStateListener
Now I want to call Firebase.authWithPassword(String, String, Firebase.AuthResultHandler);
from within an AsynTask<Void, Void, Boolean>.doInBackground(Void...params)
.
How exactly can I pass a Firebase.AuthResultHandler
to Firebase.authWithPassword(x,y,a);
without needing to create an auth handler? Is it possible? Can reference the auth handler value?
PS I am new to Firebase and trying to master extending and implementing on classes in java.
I struggled with this too. The way I did it was like you were thinking; I created a AuthHandler interface:
Note: I created a User object that I pass around
public class FirebaseServerAuthenticate implements ServerAuthenticate, Firebase.AuthResultHandler {
public static final String TAG = FirebaseServerAuthenticate.class.getSimpleName();
private ServerAuthenticateCallback callback;
private User user;
@Override
public void userSignIn(User user, ServerAuthenticateCallback callback) {
Log.d(TAG, "Firebase authentication starting: " + user);
this.user = user;
this.callback = callback;
// I check for id and token here. you may or may not need this.
if(isEmpty(user.getAuthToken()) || isEmpty(user.getUid()))
callback.onServerAuthenticateFail("User provided no auth token");
else
FireHelper.getRoot().authWithOAuthToken(AUTH_TOKEN_TYPE_GOOGLE, user.getAuthToken(), this);
}
@Override
public void onAuthenticated(AuthData authData) {
Log.d(TAG, "Firebase authentication: successful");
callback.onServerAuthenticated(user);
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
Log.e(TAG, "Firebase authentication: failed");
callback.onServerAuthenticateFail(firebaseError.getMessage());
}
}
And then you need the ServerAuthenticate
interface:
public interface ServerAuthenticate {
interface ServerAuthenticateCallback {
void onServerAuthenticated(User user);
void onServerAuthenticateFail(String error);
}
void userSignIn(final User user, ServerAuthenticateCallback callback);
}
To authenticate, use FirebaseServerAuthenticate.userSignIn
in your activity:
FirebaseServerAuthenticate serverAuthenticate = new FirebaseServerAuthenticate();
public void authenticateUser(User user){
new AsyncTask<User, Void, Void>() {
@Override
protected Void doInBackground(User... params) {
User user = params[0];
// I did some more stuff here. Got my auth token from google etc.
serverAuthenticate.userSignIn(user, new ServerAuthenticate.ServerAuthenticateCallback() {
@Override
public void onServerAuthenticated(User user) {
// Here you are authenticated!
// I kill this activity and open in to my mainActivity
finishLogin(user);
}
@Override
public void onServerAuthenticateFail(String error) {
// Handle the error. For debug, I just show an alert dialog
onError(error);
}
});
return null;
}
}.execute(user);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.