Created
October 6, 2019 02:15
-
-
Save MateusDeSousa/4ebb851bd791185f74bcb430985e9285 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
class Login : AppCompatActivity() { | |
var auth = FirebaseAuth.getInstance() | |
var username = "" | |
var password = "" | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_login) | |
// setActionButtonLogin() | |
buttonLogin.setOnClickListener { | |
username = fieldUsername.text.toString() | |
password = fieldPassword.text.toString() | |
execLogin(username, password) | |
} | |
buttonRegister.setOnClickListener { | |
actionRegister() | |
} | |
} | |
private fun actionRegister(){ | |
val intent = Intent(this, Register::class.java) | |
startActivity(intent) | |
} | |
override fun onStart() { | |
super.onStart() | |
//Verifica se o usuário ja esta logado | |
val currentUser = auth.currentUser | |
updateUI(currentUser) | |
} | |
private fun setActionButtonLogin(){ | |
buttonLogin.setOnClickListener { | |
username = fieldUsername.text.toString() | |
password = fieldPassword.text.toString() | |
execLogin(username, password) | |
} | |
} | |
private fun execLogin(username: String, password: String){ | |
if (isEmailValid(username)) { | |
if (isPasswordValid(password)){ | |
loginSession(username, password) | |
}else{ | |
Toast.makeText(this, "Senha invalida", Toast.LENGTH_LONG).show() | |
} | |
}else { | |
Toast.makeText(this, "Username invalido", Toast.LENGTH_LONG).show() | |
} | |
} | |
private fun isEmailValid(email: String): Boolean { | |
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() | |
} | |
private fun isPasswordValid(password: String): Boolean{ | |
return password.length > 8 | |
} | |
private fun loginSession(username: String, password: String) { | |
auth.signInWithEmailAndPassword(username, password) | |
.addOnCompleteListener(this) { task -> | |
if (task.isSuccessful) { | |
// Sign in success, update UI with the signed-in user's information | |
val user = auth.currentUser | |
updateUI(user) | |
} else { | |
Toast.makeText(this, "Login não executado", Toast.LENGTH_LONG).show() | |
// If sign in fails, display a message to the user. | |
} | |
// ... | |
} | |
} | |
private fun updateUI(currentUser: FirebaseUser?){ | |
if (currentUser != null) { | |
Toast.makeText(this, "Usuario logado", Toast.LENGTH_LONG).show() | |
val intent = Intent(this, MainActivity::class.java) | |
startActivity(intent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment