Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. package firebaseexample.basak.example.com.firebaseexample;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.text.TextUtils;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import com.google.android.gms.tasks.OnCompleteListener;
  14. import com.google.android.gms.tasks.Task;
  15. import com.google.firebase.auth.AuthResult;
  16. import com.google.firebase.auth.FirebaseAuth;
  17. import com.google.firebase.auth.FirebaseUser;
  18. import com.google.firebase.database.FirebaseDatabase;
  19.  
  20. public class MainActivity extends AppCompatActivity implements
  21. View.OnClickListener {
  22. Intent intent;
  23. private Button btnSave;
  24.  
  25. private Button btnLogin;
  26.  
  27. private EditText editUserName;
  28.  
  29. private EditText editUserPassword;
  30.  
  31. //db işlemleri için kullanılacaktır.
  32. private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
  33.  
  34. //auth işlemleri için kullenılacaktır.
  35. private FirebaseAuth mAuth ;
  36.  
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41.  
  42. // Views
  43.  
  44. editUserName= (EditText) findViewById(R.id.editUserName);
  45. editUserPassword = (EditText) findViewById(R.id.editUserPassword);
  46. btnLogin=(Button)findViewById(R.id.btnLogin) ;
  47. btnSave=(Button)findViewById(R.id.btnSave);
  48.  
  49. // Buttons
  50. findViewById(R.id.btnLogin).setOnClickListener(this);
  51. findViewById(R.id.btnSave).setOnClickListener(this);
  52.  
  53. // [START initialize_auth]
  54. mAuth = FirebaseAuth.getInstance();
  55. // [END initialize_auth]
  56. }
  57.  
  58. // [START on_start_check_user]
  59. @Override
  60. public void onStart() {
  61. super.onStart();
  62. // Check if user is signed in (non-null) and update UI accordingly.
  63. FirebaseUser currentUser = mAuth.getCurrentUser();
  64. updateUI(currentUser);
  65. }
  66.  
  67. private void updateUI(FirebaseUser currentUser) {
  68. }
  69. // [END on_start_check_user]
  70.  
  71. private void createAccount(String email, String password) {
  72.  
  73. if (!validateForm()) {
  74. return;
  75. }
  76.  
  77.  
  78.  
  79. // [START create_user_with_email]
  80. mAuth.createUserWithEmailAndPassword(email, password)
  81. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  82. @Override
  83. public void onComplete(@NonNull Task<AuthResult> task) {
  84. if (task.isSuccessful()) {
  85. // Sign in success, update UI with the signed-in user's information
  86.  
  87. FirebaseUser user = mAuth.getCurrentUser();
  88. updateUI(user);
  89. } else {
  90. // If sign in fails, display a message to the user.
  91.  
  92. Toast.makeText(MainActivity.this, "Authentication failed.",
  93. Toast.LENGTH_SHORT).show();
  94. updateUI(null);
  95. }
  96.  
  97. // [START_EXCLUDE]
  98.  
  99. // [END_EXCLUDE]
  100. }
  101. });
  102. // [END create_user_with_email]
  103. }
  104.  
  105. private void signIn(String email, String password) {
  106.  
  107. if (!validateForm()) {
  108. return;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. // [START sign_in_with_email]
  115. mAuth.signInWithEmailAndPassword(email, password)
  116. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  117. @Override
  118. public void onComplete(@NonNull Task<AuthResult> task) {
  119. if (task.isSuccessful()) {
  120. System.out.println("başarıyla giriş yapıldı");
  121. // Sign in success, update UI with the signed-in user's information
  122. startActivity(new Intent(MainActivity.this, MainMenuActivity.class));
  123. FirebaseUser user = mAuth.getCurrentUser();
  124. updateUI(user);
  125.  
  126. // Intent intent=new Intent(MainActivity.this,MainMenuActivity.class);
  127. //startActivity(intent);
  128. }
  129.  
  130. else {
  131. // If sign in fails, display a message to the user.
  132.  
  133. Toast.makeText(MainActivity.this, "Authentication failed.",
  134. Toast.LENGTH_LONG).show();
  135. updateUI(null);
  136.  
  137. }
  138.  
  139.  
  140. // [START_EXCLUDE]
  141.  
  142.  
  143. // [END_EXCLUDE]
  144. }
  145.  
  146. });
  147. // [END sign_in_with_email]
  148.  
  149. }
  150.  
  151. private void signOut() {
  152. mAuth.signOut();
  153. updateUI(null);
  154. }
  155.  
  156.  
  157.  
  158. private void sendEmailVerification() {
  159. // Disable button
  160.  
  161.  
  162. // Send verification email
  163. // [START send_email_verification]
  164. final FirebaseUser user = mAuth.getCurrentUser();
  165. user.sendEmailVerification()
  166. .addOnCompleteListener(this, new OnCompleteListener<Void>() {
  167. @Override
  168. public void onComplete(@NonNull Task<Void> task) {
  169. // [START_EXCLUDE]
  170. // Re-enable button
  171. if (task.isSuccessful()) {
  172. Toast.makeText(MainActivity.this,
  173. "Verification email sent to " + user.getEmail(),
  174. Toast.LENGTH_SHORT).show();
  175. } else {
  176.  
  177. Toast.makeText(MainActivity.this,
  178. "Failed to send verification email.",
  179. Toast.LENGTH_SHORT).show();
  180. }
  181. // [END_EXCLUDE]
  182. }
  183. });
  184. // [END send_email_verification]
  185. }
  186.  
  187. private boolean validateForm() {
  188. boolean valid = true;
  189.  
  190. String email = editUserName.getText().toString();
  191. if (TextUtils.isEmpty(email)) {
  192. editUserName.setError("Required.");
  193. valid = false;
  194. } else {
  195. editUserName.setError(null);
  196. }
  197.  
  198. String password = editUserPassword.getText().toString();
  199. if (TextUtils.isEmpty(password)) {
  200. editUserPassword.setError("Required.");
  201. valid = false;
  202. } else {
  203. editUserPassword.setError(null);
  204. }
  205.  
  206. return valid;
  207. }
  208.  
  209. @Override
  210. public void onClick(View v) {
  211. int i = v.getId();
  212. if (i == R.id.btnSave) {
  213. createAccount(editUserName.getText().toString(), editUserPassword.getText().toString());
  214. } else if (i == R.id.btnLogin) {
  215. signIn(editUserName.getText().toString(), editUserPassword.getText().toString());
  216. }
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement