Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. package timberwolf.blackmarket.com.blackmarket;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.graphics.PorterDuff;
  6. import android.os.Bundle;
  7. import android.support.annotation.NonNull;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.tasks.OnCompleteListener;
  17. import com.google.android.gms.tasks.Task;
  18. import com.google.firebase.auth.AuthResult;
  19. import com.google.firebase.auth.FirebaseAuth;
  20. import com.google.firebase.auth.FirebaseUser;
  21.  
  22. public class LoginActivity extends AppCompatActivity {
  23.  
  24. private static final String TAG = "LoginActivity";
  25. private FirebaseAuth mAuth;
  26. private FirebaseAuth.AuthStateListener mAuthListener;
  27. private String name;
  28. private String pass;
  29.  
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_login);
  35. setTitle("Timberwolf Black Market");
  36.  
  37. final EditText nameEdit = (EditText) findViewById(R.id.nameEdit);
  38. final EditText passwordEdit = (EditText) findViewById(R.id.passwordEdit);
  39. final EditText passwordEdit2 = (EditText) findViewById(R.id.passwordEdit2);
  40. final Button createUser = (Button) findViewById(R.id.createUser);
  41.  
  42. nameEdit.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
  43. passwordEdit.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
  44. passwordEdit2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
  45.  
  46. //start firebase code
  47. mAuth = FirebaseAuth.getInstance();
  48. mAuthListener = new FirebaseAuth.AuthStateListener() {
  49. @Override
  50. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  51. FirebaseUser user = firebaseAuth.getCurrentUser();
  52. if (user != null) {
  53. // User is signed in
  54. Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
  55. } else {
  56. // User is signed out
  57. Log.d(TAG, "onAuthStateChanged:signed_out");
  58.  
  59. //listener for create user button
  60. createUser.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. //inputs for 3 edittexts for name, password, and confirm password
  64. String input1 = nameEdit.getText().toString();
  65. String input2 = passwordEdit.getText().toString();
  66. String input3 = passwordEdit2.getText().toString();
  67.  
  68. //check if user filled in all edittexts
  69. if(TextUtils.isEmpty(input1) || TextUtils.isEmpty(input2) || TextUtils.isEmpty(input3))
  70. {
  71. Toast.makeText(LoginActivity.this, "Dude you didn't fill in all the information",
  72. Toast.LENGTH_LONG).show();
  73. }
  74. //confirm passwords are the same
  75. else if(!input2.equals(input3))
  76. {
  77. Toast.makeText(LoginActivity.this, "Passwords don't match",
  78. Toast.LENGTH_LONG).show();
  79. }
  80. else
  81. {
  82. //set name and pass to the user input
  83. name = nameEdit.getText().toString();
  84. pass = passwordEdit.getText().toString();
  85. createUser(name, pass);
  86. }
  87. }
  88. });
  89. }
  90. }
  91. };
  92.  
  93.  
  94.  
  95. }
  96.  
  97. public void createUser(String email, String password){
  98. mAuth.createUserWithEmailAndPassword(email, password)
  99. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  100. @Override
  101. public void onComplete(@NonNull Task<AuthResult> task) {
  102. Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
  103. Toast.makeText(LoginActivity.this, "successyee",
  104. Toast.LENGTH_SHORT).show();
  105. // If sign in fails, display a message to the user. If sign in succeeds
  106. // the auth state listener will be notified and logic to handle the
  107. // signed in user can be handled in the listener.
  108. if (!task.isSuccessful()) {
  109. Toast.makeText(LoginActivity.this, "Authentication failed.",
  110. Toast.LENGTH_SHORT).show();
  111. }
  112. }
  113. });
  114. }
  115.  
  116. public void signIn(){
  117. mAuth.signInWithEmailAndPassword(name, pass)
  118. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  119. @Override
  120. public void onComplete(@NonNull Task<AuthResult> task) {
  121. Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
  122.  
  123. // If sign in fails, display a message to the user. If sign in succeeds
  124. // the auth state listener will be notified and logic to handle the
  125. // signed in user can be handled in the listener.
  126. if (!task.isSuccessful()) {
  127. Log.w(TAG, "signInWithEmail", task.getException());
  128. Toast.makeText(LoginActivity.this, "Authentication failed.",
  129. Toast.LENGTH_SHORT).show();
  130. }
  131. }
  132. });
  133. }
  134.  
  135. @Override
  136. public void onStart() {
  137. super.onStart();
  138. mAuth.addAuthStateListener(mAuthListener);
  139. }
  140.  
  141. @Override
  142. public void onStop() {
  143. super.onStop();
  144. if (mAuthListener != null) {
  145. mAuth.removeAuthStateListener(mAuthListener);
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement