Commit f80abd62 by Paktalin

Linked users to database

parent 6d0def63
......@@ -28,7 +28,9 @@ dependencies {
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.firebase:firebase-client-android:2.3.1'
implementation 'com.google.firebase:firebase-firestore:17.1.0'
implementation 'com.android.support:support-annotations:28.0.0-rc02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
......@@ -36,4 +38,4 @@ dependencies {
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
\ No newline at end of file
apply plugin: 'kotlin-android-extensions'
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paktalin.vocabularynotebook" >
package="com.paktalin.vocabularynotebook">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:name=".VocabularyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".VocabularyApplication">
<activity android:name=".UserActivity"/>
android:theme="@style/AppTheme">
<activity android:name=".UserActivity" />
<activity android:name=".SignInActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
......@@ -9,7 +9,9 @@ import android.widget.Toast
import com.google.android.gms.signin.SignIn
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_sign_in.*
import com.google.firebase.firestore.FirebaseFirestore
class SignInActivity : AppCompatActivity() {
......@@ -37,7 +39,10 @@ class SignInActivity : AppCompatActivity() {
if (fieldsNotEmpty(email, password)) {
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) startUserActivity()
if (task.isSuccessful) {
Log.d(TAG, "Successfully signed in")
startUserActivity()
}
else {
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(this@SignInActivity, "Authentication failed.",
......@@ -45,7 +50,6 @@ class SignInActivity : AppCompatActivity() {
}
}
}
}
private fun signUp() {
......@@ -53,21 +57,26 @@ class SignInActivity : AppCompatActivity() {
val password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password)) {
//todo check if the password is good
// todo verify email
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) startUserActivity()
if (task.isSuccessful) {
Log.d(TAG, "Successfully signed up a new user")
addNewUserToDb(mAuth!!.currentUser!!)
startUserActivity()
}
else {
Log.w(TAG, "createUserWithEmail:failure", task.exception)
Toast.makeText(this@SignInActivity, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
}
private fun startUserActivity() {
Log.d(TAG, "Signed in successfully");
Log.d(TAG, "Signed in successfully")
val userActivityIntent = Intent(this@SignInActivity, UserActivity::class.java)
startActivity(userActivityIntent)
}
......@@ -80,6 +89,16 @@ class SignInActivity : AppCompatActivity() {
return true
}
private fun addNewUserToDb(newUser: FirebaseUser) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
val db = FirebaseFirestore.getInstance()
db.collection("users").document(newUser.uid).set(User(newUser.email))
.addOnCompleteListener({ task ->
if (task.isSuccessful) Log.i(TAG, "Successfully added user to the collection")
else Log.w(TAG, "addUser:failure", task.exception)
})
}
companion object {
private val TAG = "VN/" + SignIn::class.simpleName
}
......
package com.paktalin.vocabularynotebook;
public class User {
private String email, name;
User(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.paktalin.vocabularynotebook;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
public class UserActivity extends AppCompatActivity {
private static final String TAG = "VN/" + UserActivity.class.getSimpleName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
}
@Override
protected void onDestroy() {
super.onDestroy();
FirebaseAuth.getInstance().signOut();
}
}
package com.paktalin.vocabularynotebook
import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.activity_user.*
class UserActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_user)
printUserData()
}
override fun onDestroy() {
super.onDestroy()
FirebaseAuth.getInstance().signOut()
}
@SuppressLint("SetTextI18n")
private fun printUserData() {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
Log.d(TAG, "retrieved userId: $userId")
val db = FirebaseFirestore.getInstance()
db.collection("users").document(userId).get().addOnSuccessListener { task ->
val email = task.get("email").toString()
tvUserData.text = "email: $email"
}
}
companion object {
private val TAG = "VN/" + UserActivity::class.simpleName
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:id="@+id/tvCongrats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Congratulations! You have successfully logged in!"
app:layout_constraintBottom_toTopOf="@+id/tvUserData"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/tvUserData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/tvCongrats" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
......@@ -3,4 +3,18 @@
<string name="password_hint">Password</string>
<string name="email_hint">Email</string>
<string name="sign_in_text">Sign In</string>
<string name="title_activity_login">Sign in</string>
<!-- Strings related to login -->
<string name="prompt_email">Email</string>
<string name="prompt_password">Password (optional)</string>
<string name="action_sign_in">Sign in or register</string>
<string name="action_sign_in_short">Sign in</string>
<string name="error_invalid_email">This email address is invalid</string>
<string name="error_invalid_password">This password is too short</string>
<string name="error_incorrect_password">This password is incorrect</string>
<string name="error_field_required">This field is required</string>
<string name="permission_rationale">"Contacts permissions are needed for providing email
completions."
</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment