0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

android firebase Auth email&passwordでログインする処理memo

Last updated at Posted at 2020-07-24

#onCreateメソッド内でFirebaseAuthのインスタンスを取得しておきます

LoginActivity.kt
class LoginActivity : AppCompatActivity() {

    companion object {
        private const val RC_SIGN_IN = 120
    }

    private lateinit var loginViewModel: LoginViewModel
    private lateinit var mAuth: FirebaseAuth
    private lateinit var googleSignInClient: GoogleSignInClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        //onCreateメソッド内でFirebaseAuthのインスタンスを取得しておきます
        mAuth = FirebaseAuth.getInstance()

#ログイン処理
TaskAuthResult 操作の結果に
public Task < AuthResult > signInWithEmailAndPassword (String email、String password)
指定されたメールアドレスとパスワードでユーザーのサインイン

LoginActivity.kt
private fun loginEmailUser() {
        val email: String = username.text.toString()
        val password: String = password.text.toString()

        //入力欄が空白だったらエラーメッセージ
        if (email == "") {
            val ts = Toast.makeText(this@LoginActivity, "please write email.", Toast.LENGTH_SHORT)
            ts.setGravity(Gravity.CENTER, 0, 0)
            ts.show()
        } else if (password == "") {
            val ts = Toast.makeText(this@LoginActivity, "please write password.", Toast.LENGTH_SHORT)
            ts.setGravity(Gravity.CENTER, 0, 0)
            ts.show()
        } else {
            //ログイン処理
            mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener { task ->
                    if (task.isSuccessful) {
                        val intent = Intent(this@LoginActivity, DashboardActivity::class.java)
                        startActivity(intent)
                        finish()
                    } else {
                        val ts = Toast.makeText(
                            this@LoginActivity,
                            "Error Message: " + task.exception!!.message.toString(),
                            Toast.LENGTH_SHORT
                        )
                        ts.setGravity(Gravity.CENTER, 0, 0)
                        ts.show()
                    }
                }
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?