A library to help making login activity with Firebase
version 1.0
Almost all Android Apps have a login activity to allow users to login into their app. It is generally the first activity with which your user interacts with. For that you need to implement some logic to sign in and sign up along with designing a UI for the activity. This requires lots of unproductive and repetitive efforts to make the login smooth while checking and catching various errors and exceptions. There are tons on exceptions which needs to be caught and handled for a proper functional loginActivity.
Simple Login Library helps skip the repetitive work and lets you concentrate on the app logic by providing methods for signing in, when working with Firebase Authentication. It provides clear interface methods to catch common exceptions and makes them easily available for you to handle them (no need to search for possible exceptions over the internet).
In short it is a library to help making login activity with Firebase authentication having:
login methods for faster login activity building for android applications on java.
You can always use actual_code branch to checkout the original code without using this library!.
-
For Email and Google login:
-
Dependency:
- Add the following dependency to your project's build.gradle file:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- and this dependency in your app's build.gradle file:
dependencies {
implementation 'com.github.dev-ritik:LoginAction:84b663fe6f'
}
-
Proceed calling login classes as here
- Original methods for logging in is available here
-
Modify your manifest (if using Facebook login) from :
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"
/>
to
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"
tools:replace="android:value" />
- Check if your:
- you got your google-services.json file right.
- Project's build.gradle looks this
- App's build.gradle looks this
- Manifest looks this
- Your String.xml file has:
- facebook_app_id
- fb_login_protocol_scheme
- If any dependency added in the library is creating error, you can always add the latest in your app's build.gradle.
- Methods for:
- Email register
SimpleRegistration register = new SimpleRegistration();
register.setOnRegistrationResult(new SimpleRegistration.OnRegistrationResult() {
@Override
public void resultSuccessful(FirebaseUser registeredUser) {
//registered but not logged in
}
@Override
public void sameEmailError(Exception errorResult) {
//account exists with same email Id
}
@Override
public void networkError(Exception errorResult) {
//network error occurred
}
@Override
public void resultError(Exception errorResult) {
//some error occurred
}
@Override
public void profileUpdateError(Exception errorResult) {
//some error occurred while updating username and/or profile pic
}
@Override
public void resultName(FirebaseUser registeredUser) {
//name updated(user already registered)
}
@Override
public void resultDp(Uri dpLink) {
//DP link updated(user already registered)
}
@Override
public void wrongCredentials(String doubtfulCredential, String errorMessage) {
//doubtfulCredential : "email" or "password1" or "password2" or "password1 and password2"
//errorMessage : "empty" or "invalid" or "short" or "mismatch"
}
}
});
register.attemptRegistration(this, email, password1, password2, userNameString, downloadUrl);
- Email login
SimpleEmailLogin login = new SimpleEmailLogin();
login.setOnEmailLoginResult(new SimpleEmailLogin.OnEmailLoginResult() {
@Override
public void resultSuccessful(FirebaseUser registeredUser) {
//login successful : registeredUser
}
@Override
public void noAccountFound(Exception errorResult) {
//no account found
}
@Override
public void invalidCredentials(Exception errorResult) {
//wrong password or this is a google or facebook loggededin account");
}
@Override
public void resultError(Exception errorResult) {
//some error occurred
}
@Override
public void networkError(Exception errorResult) {
//network error occurred
}
@Override
public void wrongCredentials(String doubtfulCredentials, String errorMessage) {
//doubtfulCredentials : "email" or "password"
//errorMessage : "empty" or "invalid" or "short"
}
});
login.attemptLogin(this, email, password);
- Password changing
SimpleEmailLogin passwordReset = new SimpleEmailLogin();
passwordReset.setOnPasswordChangeResult(new SimpleEmailLogin.OnPasswordChangeResult() {
@Override
public void resultSuccessful() {
//Check your email for a reset link
}
@Override
public void noAccountFound(Exception errorResult) {
//no account found
}
@Override
public void resultError(Exception errorResult) {
//some error occurred
}
@Override
public void networkError(Exception errorResult) {
//network error occurred
}
@Override
public void wrongEmail(String errorMessage) {
//errorMessage : "empty" or "invalid"
}
});
passwordReset.attemptPasswordReset(email);
- Google login
googleLogin = new SimpleGoogleLogin(this, RC_SIGN_IN_GOOGLE, getString(R.string.default_web_client_id));
googleLogin.setOnGoogleLoginResult(new SimpleGoogleLogin.OnGoogleLoginResult() {
@Override
public void resultSuccessful(FirebaseUser registeredUser) {
//login successful
}
@Override
public void signinCancelledByUser(Exception errorResult) {
//signin cancelled by user
}
@Override
public void accountCollisionError(Exception errorResult) {
//account exists with same email Id
}
@Override
public void networkError(Exception errorResult) {
//network error occurred
}
@Override
public void resultError(Exception errorResult) {
//error occurred
}
});
googleLogin.attemptGoogleLogin();
//googleLogin is the instance of SimpleGoogleLogin
- In onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN_GOOGLE) {
googleLogin.onActivityResult(requestCode, resultCode, data);
}
}
//googleLogin is the instance of same SimpleGoogleLogin
- Facebook login (call it in oncreate method)
- It has an important limitation discussed here
facebookLogin = new SimpleFacebookLogin(this);
facebookLogin.setOnFacebookLoginResult(new SimpleFacebookLogin.OnFacebookLoginResult() {
@Override
public void resultFacebookLoggedIn(LoginResult loginResult) {
//Facebook login successful, yet to authenticate Firebase
}
@Override
public void resultActualLoggedIn(FirebaseUser registeredUser) {
//Facebook and Firebase login successful
}
@Override
public void resultCancel() {
//Facebook login cancelled
}
@Override
public void accountCollisionError(Exception errorResult) {
//account already exists with the different sign-in credentials
}
@Override
public void networkError(Exception errorResult) {
//network error occurred
}
@Override
public void resultError(Exception errorResult) {
//Facebook or Firebase login error
}
});
facebookLogin.attemptFacebookLogin(mloginButton);
//mloginButton is LoginButton from FacebookButtonBase
//facebookLogin is the instance of SimpleFacebookLogin
- In onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (!(facebookLogin == null)){
facebookLogin.onActivityResult(requestCode, resultCode, data);
}
}
//facebookLogin is the instance of same SimpleFacebookLogin
- Signout
AuthUI.getInstance().signOut(this);
Arguments for wrong credentials
Method | library class | doubtfulCredentials | errorMessage |
---|---|---|---|
Registration | SimpleRegistration | empty | |
Registration | SimpleRegistration | invalid | |
Registration | SimpleRegistration | password1 | empty |
Registration | SimpleRegistration | password1 | short |
Registration | SimpleRegistration | password2 | empty |
Registration | SimpleRegistration | password2 | short |
Registration | SimpleRegistration | password1 and password2 | mismatch |
Email login | SimpleEmailLogin | empty | |
Email login | SimpleEmailLogin | invalid | |
Email login | SimpleEmailLogin | password | empty |
Email login | SimpleEmailLogin | password | short |
Password reset | SimpleEmailLogin | ---- | empty |
Password reset | SimpleEmailLogin | ---- | invalid |
All contributions are welcome and appreciated. Please open an issue or make a Pull Request, if necessary. This may also include any new error to be caught or any form of feature enhancement. Every constructive criticism is welcome.