|  | 
|  | 1 | +package org.codedocs.codedocsapp | 
|  | 2 | + | 
|  | 3 | +import android.app.PendingIntent.getActivity | 
|  | 4 | +import android.content.Intent | 
|  | 5 | +import android.support.v7.app.AppCompatActivity | 
|  | 6 | +import android.os.Bundle | 
|  | 7 | +import android.preference.PreferenceManager | 
|  | 8 | +import android.util.Log | 
|  | 9 | +import android.widget.Button | 
|  | 10 | +import android.widget.TextView | 
|  | 11 | +import android.widget.Toast | 
|  | 12 | +import com.google.android.gms.tasks.OnCompleteListener | 
|  | 13 | +import com.google.android.gms.tasks.Task | 
|  | 14 | +import com.google.firebase.auth.AuthResult | 
|  | 15 | +import com.google.firebase.auth.FirebaseAuth | 
|  | 16 | +import kotlinx.android.synthetic.main.activity_login.* | 
|  | 17 | +import java.security.AccessController.getContext | 
|  | 18 | + | 
|  | 19 | +class Login : AppCompatActivity() { | 
|  | 20 | + var mAuth: FirebaseAuth?=null | 
|  | 21 | + | 
|  | 22 | + override fun onCreate(savedInstanceState: Bundle?) { | 
|  | 23 | + super.onCreate(savedInstanceState) | 
|  | 24 | + setContentView(R.layout.activity_login) | 
|  | 25 | + mAuth=FirebaseAuth.getInstance() | 
|  | 26 | + val button=findViewById<Button>(R.id.loginButton) | 
|  | 27 | + button.setOnClickListener{ | 
|  | 28 | + verification() | 
|  | 29 | + } | 
|  | 30 | + val already=findViewById<TextView>(R.id.registernow) | 
|  | 31 | + already.setOnClickListener{ | 
|  | 32 | + register() | 
|  | 33 | + } | 
|  | 34 | + | 
|  | 35 | + } | 
|  | 36 | + private fun register(){ | 
|  | 37 | + val intent= Intent(this@Login,Register::class.java) | 
|  | 38 | + startActivity(intent) | 
|  | 39 | + } | 
|  | 40 | + private fun verification(){ | 
|  | 41 | + val email=email_login.text.toString() | 
|  | 42 | + val password=password_login.text.toString() | 
|  | 43 | + if(email.isEmpty()){ | 
|  | 44 | + Toast.makeText(this,"Email Address Empty",Toast.LENGTH_LONG).show() | 
|  | 45 | + return | 
|  | 46 | + } | 
|  | 47 | + if(password.length<=6){ | 
|  | 48 | + Toast.makeText(this,"Password Short",Toast.LENGTH_LONG).show() | 
|  | 49 | + return | 
|  | 50 | + } | 
|  | 51 | + signin() | 
|  | 52 | + | 
|  | 53 | + } | 
|  | 54 | + private fun signin(){ | 
|  | 55 | + val email=email_login.text.toString() | 
|  | 56 | + val password=password_login.text.toString() | 
|  | 57 | + mAuth!!.signInWithEmailAndPassword(email,password) | 
|  | 58 | + .addOnCompleteListener(this, OnCompleteListener<AuthResult> {task -> | 
|  | 59 | + if (task.isSuccessful) { | 
|  | 60 | + // Sign in success, update UI with the signed-in user's information | 
|  | 61 | + Log.d("signin", "signInWithEmail:success"); | 
|  | 62 | + val user = mAuth!!.currentUser; | 
|  | 63 | + val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) | 
|  | 64 | + val editor = preferences.edit() | 
|  | 65 | + editor.putString("email",email) | 
|  | 66 | + editor.putBoolean("status",true) | 
|  | 67 | + editor.putString("userid",user!!.uid) | 
|  | 68 | + editor.commit() | 
|  | 69 | + val intent= Intent(this@Login,MainActivity::class.java) | 
|  | 70 | + startActivity(intent) | 
|  | 71 | + | 
|  | 72 | + } else { | 
|  | 73 | + // If sign in fails, display a message to the user. | 
|  | 74 | + Log.w("signin", "signInWithEmail:failure", task.getException()); | 
|  | 75 | + Toast.makeText(this, "Authentication failed.", | 
|  | 76 | + Toast.LENGTH_SHORT).show() | 
|  | 77 | + } | 
|  | 78 | + } | 
|  | 79 | + ) | 
|  | 80 | + | 
|  | 81 | + | 
|  | 82 | + | 
|  | 83 | + } | 
|  | 84 | +} | 
0 commit comments