Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
- private Button buttonSignIn;
- private EditText editTextEmail;
- private EditText editTextPassword;
- private TextView textViewSignUp;
- private ProgressDialog progressDialog;
- private FirebaseAuth firebaseAuth;
- private CallbackManager mCallbackManager;
- private static final String TAG = "FACELOG";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- firebaseAuth = FirebaseAuth.getInstance();
- if(firebaseAuth.getCurrentUser() != null) {
- //profile activity here (user already logged in)
- finish();
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }
- progressDialog = new ProgressDialog(this);
- editTextEmail = (EditText) findViewById(R.id.editTextEmail);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- buttonSignIn = (Button) findViewById(R.id.buttonSignIn);
- textViewSignUp = (TextView) findViewById(R.id.textViewSignUp);
- buttonSignIn.setOnClickListener(this);
- textViewSignUp.setOnClickListener(this);
- // Initialize Facebook Login button
- mCallbackManager = CallbackManager.Factory.create();
- LoginButton loginButton = findViewById(R.id.login_button);
- loginButton.setReadPermissions("email", "public_profile");
- loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
- @Override
- public void onSuccess(LoginResult loginResult) {
- Log.d(TAG, "facebook:onSuccess:" + loginResult);
- handleFacebookAccessToken(loginResult.getAccessToken());
- }
- @Override
- public void onCancel() {
- Log.d(TAG, "facebook:onCancel");
- }
- @Override
- public void onError(FacebookException error) {
- Log.d(TAG, "facebook:onError", error);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- // Pass the activity result back to the Facebook SDK
- mCallbackManager.onActivityResult(requestCode, resultCode, data);
- }
- private void userLogin() {
- String email = editTextEmail.getText().toString().trim();
- String password = editTextPassword.getText().toString().trim();
- if(TextUtils.isEmpty(email)) { //email is empty
- Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
- return;
- }
- if(TextUtils.isEmpty(password)) { //password is empty
- Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
- }
- //if validation is okay
- //we will first show a progress bar
- progressDialog.setMessage("Logging In...");
- progressDialog.show();
- firebaseAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- progressDialog.dismiss();
- if(task.isSuccessful()) {
- //start profile activity
- finish();
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }
- }
- });
- }
- @Override
- public void onClick(View view) {
- if(view == buttonSignIn) { userLogin(); }
- if(view == textViewSignUp) {
- finish();
- startActivity(new Intent(this, SignupActivity.class));
- }
- }
- @Override
- public void onStart() {
- super.onStart();
- // Check if user is signed in (non-null) and update UI accordingly.
- FirebaseUser currentUser = firebaseAuth.getCurrentUser();
- if(currentUser != null) {
- updateUI();
- }
- }
- private void handleFacebookAccessToken(AccessToken token) {
- Log.d(TAG, "handleFacebookAccessToken:" + token);
- AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
- firebaseAuth.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");
- FirebaseUser user = firebaseAuth.getCurrentUser();
- updateUI();
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithCredential:failure", task.getException());
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- //updateUI();
- }
- // ...
- }
- });*/
- updateUI();
- }
- private void updateUI() {
- Toast.makeText(LoginActivity.this, "You logged in with FB.",
- Toast.LENGTH_LONG).show();
- finish();
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement