Created
May 28, 2018 18:00
-
-
Save JobGetabu/75151cdf9f1abdc68927bf66fb139c74 to your computer and use it in GitHub Desktop.
Firebase Auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //firebase email password auth in fragments | |
| public class EmailPasswordLogInFragment extends Fragment { | |
| public static final String TAG = "EmailPassLogInFragment"; | |
| private View mRootView; | |
| private FirebaseAuth mAuth; | |
| public EmailPasswordLogInFragment() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| //implement logic on no network | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| // Inflate the layout for this fragment | |
| mRootView = inflater.inflate(R.layout.fragment_email_password_log_in, container, false); | |
| //firebase | |
| mAuth = FirebaseAuth.getInstance(); | |
| mFirestore = FirebaseFirestore.getInstance(); | |
| return mRootView; | |
| } | |
| // call this in onClick of sign in button | |
| //@OnClick(R.id.login_btn_signup) | |
| public void loginWithEmailPasswordClick() { | |
| String email = logEmail.getEditText().getText().toString(); | |
| String password = mPassword.getEditText().getText().toString(); | |
| Log.d(TAG, "loginWithEmailPasswordClick: Email & password:" + email + " " + password); | |
| //TODO: | |
| //check connection | |
| //start a progress dialogue | |
| mAuth.signInWithEmailAndPassword(email, password) | |
| .addOnCompleteListener(new OnCompleteListener<AuthResult>() { | |
| @Override | |
| public void onComplete(@NonNull Task<AuthResult> authtask) { | |
| if (authtask.isSuccessful()) { | |
| //login successful | |
| //dismiss a progress dialogue | |
| } else { | |
| //login unsuccessful | |
| //dismiss a progress dialogue | |
| UserAuthToastExceptions(authtask); | |
| } | |
| } | |
| }); | |
| } else { | |
| // Error prompting logic ... Done! | |
| } | |
| } | |
| private void errorPrompt() { | |
| //TODO: A toast,snack or something fancy ;) | |
| } | |
| private void errorPrompt(String title, String message) { | |
| //TODO: A toast,snack or something fancy ;) | |
| } | |
| private void UserAuthToastExceptions(@NonNull Task<AuthResult> authtask) { | |
| String error = ""; | |
| try { | |
| throw authtask.getException(); | |
| } catch (FirebaseAuthWeakPasswordException e) { | |
| error = "Weak Password!"; | |
| } catch (FirebaseAuthInvalidCredentialsException e) { | |
| error = "Invalid email"; | |
| } catch (FirebaseAuthUserCollisionException e) { | |
| error = "Existing Account"; | |
| } catch (Exception e) { | |
| error = "Unknown Error Occured"; | |
| e.printStackTrace(); | |
| } | |
| //Toast.makeText(getActivity(), error, Toast.LENGTH_LONG).show(); | |
| errorPrompt("Oops...", error); | |
| } | |
| public boolean validate() { | |
| boolean valid = true; | |
| final String email = logEmail.getEditText().getText().toString().trim(); | |
| String password = mPassword.getEditText().getText().toString(); | |
| if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { | |
| logEmail.setError("enter a valid email address"); | |
| valid = false; | |
| } else { | |
| logEmail.setError(null); | |
| } | |
| if (password.isEmpty() || password.length() < 6) { | |
| mPassword.setError("at least 6 characters"); | |
| valid = false; | |
| } else { | |
| mPassword.setError(null); | |
| } | |
| Log.d(TAG, "validate: " + valid + "-> " + email); | |
| return valid; | |
| } | |
| @Override | |
| public void onDestroy() { | |
| //TODO: release other heavy resources ;( | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment