Advertisement
Checosnake

SignUp

Oct 30th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. package com.sigma.sanunez.firebaseauthentication;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.text.TextUtils;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.google.android.gms.tasks.OnCompleteListener;
  15. import com.google.android.gms.tasks.Task;
  16. import com.google.firebase.auth.AuthResult;
  17. import com.google.firebase.auth.FirebaseAuth;
  18. import com.google.firebase.auth.FirebaseUser;
  19.  
  20. import android.os.Bundle;
  21.  
  22. public class Signup extends AppCompatActivity implements View.OnClickListener {
  23.     private static final String TAG = "EmailPassword";
  24.     private EditText Textemail;
  25.     private EditText TextPassword;
  26.     private EditText TextPassword2;
  27.     private FirebaseAuth mAuth;
  28.     private FirebaseAuth.AuthStateListener mAuthListener;
  29.     String email, pass, password2;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_signup);
  35.  
  36.         Textemail = (EditText) findViewById(R.id.editText5);
  37.         TextPassword = (EditText) findViewById(R.id.editText7);
  38.         TextPassword2 = (EditText) findViewById(R.id.editText8);
  39.  
  40.         findViewById(R.id.button2).setOnClickListener(this);
  41.  
  42.         mAuth = FirebaseAuth.getInstance();
  43.         mAuthListener = new FirebaseAuth.AuthStateListener() {
  44.             @Override
  45.             public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  46.                 FirebaseUser user = firebaseAuth.getCurrentUser();
  47.                 if (user != null) {
  48.                     // User is signed in
  49.                     Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
  50.                     Toast.makeText(Signup.this, "Thank's For Signing Up", Toast.LENGTH_SHORT).show();
  51.                     toMain();
  52.                 } else {
  53.                     // User is signed out
  54.                     Log.d(TAG, "onAuthStateChanged:signed_out");
  55.                 }
  56.  
  57.             }
  58.         };
  59.  
  60.     }
  61.  
  62.     private void toMain() {
  63.         Intent myIntent = new Intent(this, MainActivity.class);
  64.         startActivity(myIntent);
  65.         finish();
  66.     }
  67.  
  68.     private boolean validateForm() {
  69.         boolean valid = true;
  70.         String email = Textemail.getText().toString();
  71.         String password = TextPassword.getText().toString();
  72.         String password2 = TextPassword2.getText().toString();
  73.         if (TextUtils.isEmpty(email)) {
  74.             Textemail.setError("Required.");
  75.             valid = false;
  76.         } else {
  77.             Textemail.setError(null);
  78.         }
  79.         if (TextUtils.isEmpty(password)) {
  80.             TextPassword.setError("Required.");
  81.             valid = false;
  82.         } else {
  83.             TextPassword.setError(null);
  84.         }
  85.         if (TextUtils.isEmpty(password2)) {
  86.             TextPassword2.setError("Required.");
  87.             valid = false;
  88.         } else {
  89.             TextPassword2.setError(null);
  90.         }
  91.  
  92.         if(TextUtils.equals(password, password2)) {
  93.             TextPassword.setError(null);
  94.             TextPassword2.setError(null);
  95.         } else {
  96.             TextPassword.setError("Passwords must match.");
  97.             TextPassword2.setError("Passwords must match.");
  98.             valid = false;
  99.         }
  100.         return valid;
  101.     }
  102.  
  103.     private void signOut() {
  104.         mAuth.signOut();
  105.     }
  106.  
  107.     private void signIn(String email, String password) {
  108.         Log.d(TAG, "signIn:" + email);
  109.         if (!validateForm()) {
  110.             return;
  111.         }
  112.  
  113.         // [START sign_in_with_email]
  114.         mAuth.signInWithEmailAndPassword(email, password)
  115.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  116.                     @Override
  117.                     public void onComplete(@NonNull Task<AuthResult> task) {
  118.                         Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
  119.  
  120.                         // If sign in fails, display a message to the user. If sign in succeeds
  121.                         // the auth state listener will be notified and logic to handle the
  122.                         // signed in user can be handled in the listener.
  123.                         if (!task.isSuccessful()) {
  124.                             Log.w(TAG, "signInWithEmail:failed", task.getException());
  125.                             Toast.makeText(Signup.this, R.string.auth_failed,
  126.                                     Toast.LENGTH_SHORT).show();
  127.                         }
  128.  
  129.                         // [START_EXCLUDE]
  130.                         if (!task.isSuccessful()) {
  131.                             //mStatusTextView.setText(R.string.auth_failed);
  132.                         }
  133.  
  134.                     }
  135.                 });
  136.         // [END sign_in_with_email]
  137.     }
  138.  
  139.     private void createAccount(String email, String password) {
  140.         Log.d(TAG, "createAccount:" + email);
  141.         if (!validateForm()) {
  142.             return;
  143.         }
  144.  
  145.         // [START create_user_with_email]
  146.         mAuth.createUserWithEmailAndPassword(email, password)
  147.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  148.                     @Override
  149.                     public void onComplete(@NonNull Task<AuthResult> task) {
  150.                         Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
  151.  
  152.                         // If sign in fails, display a message to the user. If sign in succeeds
  153.                         // the auth state listener will be notified and logic to handle the
  154.                         // signed in user can be handled in the listener.
  155.                         if (!task.isSuccessful()) {
  156.                             Toast.makeText(Signup.this, R.string.auth_failed,
  157.                                     Toast.LENGTH_SHORT).show();
  158.                         }
  159.  
  160.                     }
  161.                 });
  162.         // [END create_user_with_email]
  163.     }
  164.  
  165.     @Override
  166.     public void onStop() {
  167.         super.onStop();
  168.         if (mAuthListener != null) {
  169.             mAuth.removeAuthStateListener(mAuthListener);
  170.         }
  171.     }
  172.  
  173.     @Override
  174.     public void onStart() {
  175.         super.onStart();
  176.         mAuth.addAuthStateListener(mAuthListener);
  177.     }
  178.  
  179.     @Override
  180.     public void onClick(View v) {
  181.         int i = v.getId();
  182.         if (i == R.id.button2) {
  183.             email = Textemail.getText().toString();
  184.             pass = TextPassword.getText().toString();
  185.             createAccount(email,pass);
  186.         }
  187.  
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement