Skip to content

Commit 8ec9a72

Browse files
authored
Merge pull request #10 from Abhimanyu121/master
added login and register
2 parents e76c63c + a0d87e8 commit 8ec9a72

File tree

7 files changed

+244
-5
lines changed

7 files changed

+244
-5
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535

3636
<activity
3737
android:name=".Register"
38-
android:label="@string/title_activity_register"></activity>
38+
android:label="@string/register_activity">
39+
40+
</activity>
41+
<activity android:name=".Login"></activity>
3942
</application>
4043

4144
</manifest>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

app/src/main/java/org/codedocs/codedocsapp/MainActivity.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.codedocs.codedocsapp
33

44
import android.content.Intent
55
import android.os.Bundle
6+
import android.preference.PreferenceManager
67
import android.support.design.widget.BottomNavigationView
78
import android.support.v4.app.Fragment
89
import android.support.v7.app.AppCompatActivity
@@ -81,6 +82,12 @@ class MainActivity : AppCompatActivity() {
8182
override fun onCreate(savedInstanceState: Bundle?) {
8283
super.onCreate(savedInstanceState)
8384
setContentView(R.layout.activity_main)
85+
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
86+
val status = preferences.getBoolean("status", false)
87+
if (status==false){
88+
val intent = Intent(this,Login::class.java)
89+
startActivity(intent)
90+
}
8491
val fm=supportFragmentManager
8592
val ft=fm.beginTransaction()
8693
val fragment=home()

app/src/main/java/org/codedocs/codedocsapp/Register.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,86 @@ import android.widget.TextView
2323

2424
import java.util.ArrayList
2525
import android.Manifest.permission.READ_CONTACTS
26+
import android.content.Intent
27+
import android.preference.PreferenceManager
28+
import android.util.Log
29+
import android.widget.Button
30+
import android.widget.Toast
31+
import com.google.android.gms.tasks.OnCompleteListener
32+
import com.google.android.gms.tasks.Task
33+
import com.google.firebase.auth.AuthResult
34+
import com.google.firebase.auth.FirebaseAuth
2635

2736
import kotlinx.android.synthetic.main.activity_register.*
2837

2938
/**
3039
* A login screen that offers login via email/password.
3140
*/
3241
class Register : AppCompatActivity(){
42+
private var mAuth: FirebaseAuth?=null
3343

3444

3545
override fun onCreate(savedInstanceState: Bundle?) {
3646
super.onCreate(savedInstanceState)
3747
setContentView(R.layout.activity_register)
48+
mAuth=FirebaseAuth.getInstance()
49+
val button=findViewById<Button>(R.id.registerButton)
50+
button.setOnClickListener{
51+
verification()
52+
}
53+
val already=findViewById<TextView>(R.id.already_member)
54+
already.setOnClickListener(){
55+
signin()
56+
}
3857

3958
}
59+
private fun signin(){
60+
val intent= Intent(this@Register,Login::class.java)
61+
startActivity(intent)
62+
}
63+
private fun verification(){
64+
val email=email_register.text.toString()
65+
val password=password_registration.text.toString()
66+
val conf=confirm_password.text.toString()
67+
if(email.isEmpty()){
68+
Toast.makeText(this,"Email Address Empty",Toast.LENGTH_LONG).show()
69+
return
70+
}
71+
if(password.length<=6){
72+
Toast.makeText(this,"Password Short",Toast.LENGTH_LONG).show()
73+
return
74+
}
75+
if(password != conf){
76+
Toast.makeText(this,"Confirmation Password do not Match",Toast.LENGTH_LONG).show()
77+
return
78+
}
79+
register()
80+
}
81+
private fun register(){
82+
val email=email_register.text.toString()
83+
val password=password_registration.text.toString()
84+
mAuth!!.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this,object :OnCompleteListener<AuthResult>{
85+
override fun onComplete(task: Task<AuthResult>) {
86+
if(task.isSuccessful){
87+
val user=mAuth!!.currentUser
88+
val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
89+
val editor = preferences.edit()
90+
editor.putString("email",email)
91+
editor.putBoolean("status",true)
92+
editor.putString("userid",user!!.uid)
93+
editor.commit()
94+
val intent= Intent(this@Register,MainActivity::class.java)
95+
startActivity(intent)
96+
}
97+
else {
98+
Log.e( "createUserWithEmail", task.exception!!.toString());
99+
Toast.makeText(applicationContext, task.exception!!.toString(),
100+
Toast.LENGTH_SHORT).show()
101+
102+
}
103+
}
104+
})
105+
}
40106

41107

42108
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:gravity="center_horizontal"
7+
android:orientation="vertical"
8+
android:paddingBottom="@dimen/activity_vertical_margin"
9+
android:paddingLeft="@dimen/activity_horizontal_margin"
10+
android:paddingRight="@dimen/activity_horizontal_margin"
11+
android:paddingTop="@dimen/activity_vertical_margin"
12+
tools:context="org.codedocs.codedocsapp.Login">
13+
<!-- Login progress -->
14+
<ProgressBar
15+
android:id="@+id/login_progress"
16+
style="?android:attr/progressBarStyleLarge"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:layout_marginBottom="8dp"
20+
android:visibility="gone" />
21+
22+
<ScrollView
23+
android:id="@+id/login_form"
24+
android:layout_width="match_parent"
25+
android:layout_height="match_parent">
26+
27+
<LinearLayout
28+
android:id="@+id/email_login_form"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
android:orientation="vertical">
32+
33+
34+
<EditText
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:id="@+id/email_login"
38+
android:hint="@string/email_id"
39+
android:inputType="textEmailAddress"/>
40+
<EditText
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:id="@+id/password_login"
44+
android:hint="@string/password"
45+
android:inputType="textPassword"
46+
/>
47+
48+
<Button
49+
android:id="@+id/loginButton"
50+
style="?android:textAppearanceSmall"
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content"
53+
android:layout_marginTop="16dp"
54+
android:text="@string/login"
55+
android:textStyle="bold" />
56+
<TextView
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:textAlignment="center"
60+
android:text="@string/register_click_here"
61+
android:id="@+id/registernow" />
62+
63+
64+
</LinearLayout>
65+
</ScrollView>
66+
</LinearLayout>

app/src/main/res/layout/activity_register.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,30 @@
3535
android:layout_width="match_parent"
3636
android:layout_height="wrap_content"
3737
android:id="@+id/email_register"
38-
android:text="@string/email_id"
38+
android:hint="@string/email_id"
3939
android:inputType="textEmailAddress"/>
4040
<EditText
4141
android:layout_width="match_parent"
4242
android:layout_height="wrap_content"
4343
android:id="@+id/password_registration"
44-
android:hint="Password"
44+
android:hint="@string/password"
45+
android:inputType="textPassword"
46+
/>
47+
<EditText
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:id="@+id/confirm_password"
51+
android:hint="@string/confirm_password"
4552
android:inputType="textPassword"
4653
/>
4754

4855
<Button
49-
android:id="@+id/email_sign_in_button"
56+
android:id="@+id/registerButton"
5057
style="?android:textAppearanceSmall"
5158
android:layout_width="match_parent"
5259
android:layout_height="wrap_content"
5360
android:layout_marginTop="16dp"
54-
android:text="@string/action_sign_in"
61+
android:text="@string/resgister"
5562
android:textStyle="bold" />
5663
<TextView
5764
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@
2727
<string name="title_activity_register">Sign in</string>
2828
<string name="email_id">Email ID</string>
2929
<string name="already_a_member_sing_in">Already a member? Sing in</string>
30+
<string name="resgister">Resgister</string>
31+
<string name="confirm_password">Confirm Password</string>
32+
<string name="password">Password</string>
33+
<string name="register_activity">Register</string>
34+
<string name="login">Login</string>
35+
<string name="register_click_here">Register? Click here</string>
3036
</resources>

0 commit comments

Comments
 (0)