Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package timberwolf.blackmarket.com.blackmarket;
- import android.content.Intent;
- import android.graphics.Color;
- import android.graphics.PorterDuff;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- public class LoginActivity extends AppCompatActivity {
- private static final String TAG = "LoginActivity";
- private FirebaseAuth mAuth;
- private FirebaseAuth.AuthStateListener mAuthListener;
- private String name;
- private String pass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- setTitle("Timberwolf Black Market");
- final EditText nameEdit = (EditText) findViewById(R.id.nameEdit);
- final EditText passwordEdit = (EditText) findViewById(R.id.passwordEdit);
- final EditText passwordEdit2 = (EditText) findViewById(R.id.passwordEdit2);
- final Button createUser = (Button) findViewById(R.id.createUser);
- nameEdit.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
- passwordEdit.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
- passwordEdit2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
- //start firebase code
- mAuth = FirebaseAuth.getInstance();
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user != null) {
- // User is signed in
- Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
- } else {
- // User is signed out
- Log.d(TAG, "onAuthStateChanged:signed_out");
- //listener for create user button
- createUser.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //inputs for 3 edittexts for name, password, and confirm password
- String input1 = nameEdit.getText().toString();
- String input2 = passwordEdit.getText().toString();
- String input3 = passwordEdit2.getText().toString();
- //check if user filled in all edittexts
- if(TextUtils.isEmpty(input1) || TextUtils.isEmpty(input2) || TextUtils.isEmpty(input3))
- {
- Toast.makeText(LoginActivity.this, "Dude you didn't fill in all the information",
- Toast.LENGTH_LONG).show();
- }
- //confirm passwords are the same
- else if(!input2.equals(input3))
- {
- Toast.makeText(LoginActivity.this, "Passwords don't match",
- Toast.LENGTH_LONG).show();
- }
- else
- {
- //set name and pass to the user input
- name = nameEdit.getText().toString();
- pass = passwordEdit.getText().toString();
- createUser(name, pass);
- }
- }
- });
- }
- }
- };
- }
- public void createUser(String email, String password){
- mAuth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
- Toast.makeText(LoginActivity.this, "successyee",
- Toast.LENGTH_SHORT).show();
- // If sign in fails, display a message to the user. If sign in succeeds
- // the auth state listener will be notified and logic to handle the
- // signed in user can be handled in the listener.
- if (!task.isSuccessful()) {
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void signIn(){
- mAuth.signInWithEmailAndPassword(name, pass)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
- // If sign in fails, display a message to the user. If sign in succeeds
- // the auth state listener will be notified and logic to handle the
- // signed in user can be handled in the listener.
- if (!task.isSuccessful()) {
- Log.w(TAG, "signInWithEmail", task.getException());
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- public void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListener);
- }
- @Override
- public void onStop() {
- super.onStop();
- if (mAuthListener != null) {
- mAuth.removeAuthStateListener(mAuthListener);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement