Created
February 25, 2017 18:56
-
-
Save Danil0v3s/e69bce20a4d6d1598154dc6e43544342 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```java | |
package br.com.firstsoft.escolasbrasil.activitiy; | |
import android.content.Intent; | |
import android.support.annotation.NonNull; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
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; | |
import java.io.Serializable; | |
import br.com.firstsoft.escolasbrasil.R; | |
public class MainActivity extends AppCompatActivity { | |
private Button btnEntrar; | |
private Button btnCadastrar; | |
private EditText textEmail; | |
private EditText textSenha; | |
private FirebaseAuth firebaseAuth; | |
private FirebaseAuth.AuthStateListener firebaseAuthListener; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
firebaseAuth = FirebaseAuth.getInstance(); | |
configureAuthListener(); | |
btnEntrar = (Button) findViewById(R.id.btn_entrar); | |
btnCadastrar = (Button) findViewById(R.id.btn_cadastrar); | |
textEmail = (EditText) findViewById(R.id.input_login_email); | |
textSenha = (EditText) findViewById(R.id.input_login_senha); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
firebaseAuth.addAuthStateListener(firebaseAuthListener); | |
btnEntrar.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if ( !(textSenha.toString().isEmpty() && textEmail.toString().isEmpty()) ) { | |
doLogin(textEmail.getText().toString(), textSenha.getText().toString()); | |
} | |
} | |
}); | |
btnCadastrar.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(MainActivity.this, CadastrarActivity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
if (firebaseAuthListener != null) { | |
firebaseAuth.removeAuthStateListener(firebaseAuthListener); | |
} | |
} | |
public void configureAuthListener() { | |
firebaseAuthListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
FirebaseUser user = firebaseAuth.getCurrentUser(); | |
if (user != null) { | |
Intent intent = new Intent(MainActivity.this, HomeActivity.class); | |
startActivity(intent); | |
} else { | |
Log.d("FIREBASE AUTH", "onAuthStateChanged:signed_out"); | |
} | |
} | |
}; | |
} | |
public void doLogin(String email, String senha) { | |
firebaseAuth.signInWithEmailAndPassword(email, senha).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (!task.isSuccessful()) { | |
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment