Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Laurislopata/signin #52

Merged
merged 25 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
implementation 'com.google.firebase:firebase-auth-ktx:21.0.2'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.6'
testImplementation 'androidx.test:core:1.4.0'
Expand All @@ -66,6 +67,7 @@ dependencies {
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
implementation ('com.github.bumptech.glide:glide:4.13.0', {
exclude group: 'com.android.support'
})
Expand Down Expand Up @@ -109,4 +111,4 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea

connectedCheck {
finalizedBy jacocoTestReport
}
}
10 changes: 9 additions & 1 deletion app/google-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
}
},
"oauth_client": [
{
"client_id": "7687769601-4jlovmab1k6hbdq12ik9mlp7m0789ibs.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "ch.sdp.vibester",
"certificate_hash": "2d58d515333158e4e3bdd8157eb8d4d4df103cb0"
}
},
{
"client_id": "7687769601-qiqrp6kt48v89ub76k9lkpefh9ls36ha.apps.googleusercontent.com",
"client_type": 3
Expand All @@ -36,4 +44,4 @@
}
],
"configuration_version": "1"
}
}
89 changes: 89 additions & 0 deletions app/src/androidTest/java/ch/sdp/vibester/SignInActivityTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ch.sdp.vibester

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import ch.sdp.vibester.auth.FireBaseAuthenticator
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.random.Random


/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class SignInActivityTest {

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ch.sdp.vibester", appContext.packageName)
}

@get:Rule
val testRule = ActivityScenarioRule(
Register::class.java
)


@Test
fun logInCorrect() {
val username = "[email protected]"
val password = "password"
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.logIn)).perform(click())
Thread.sleep(3_000)
onView(withId(R.id.email)).check(matches(withText("[email protected]")))
}

@Test
fun logInIncorrect() {
val username = "[email protected]"
val password = "password"
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.logIn)).perform(click())
Thread.sleep(3_000)
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
}

@Test
fun createAccountIncorrect() {
val username = "[email protected]"
val password = "password"
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())
Thread.sleep(3_000)
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
}

@Test
fun createAccountCorrect() {
val randomInt = Random.nextInt(0, 10000)
val password = "password"
val username = randomInt.toString().plus("@gg.com")
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())
Thread.sleep(3_000)
onView(withId(R.id.email)).check(matches(withText(randomInt.toString().plus("@gg.com"))))
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class WelcomeScreenTest {
@Test
fun checkIntentOnSettings(){ //FILLER TESTING
onView(withId(R.id.welcome_settings)).perform(click())
intended(hasComponent(GameSetupScreen::class.java.name))
intended(hasComponent(Register::class.java.name))
}

/*
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ch.sdp.vibester">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


<application
android:allowBackup="true"
Expand All @@ -19,6 +22,9 @@
<activity
android:name=".WelcomeScreen"
android:exported="false" />
<activity
android:name=".Register"
android:exported="true" />
<activity
android:name=".GreetingActivity"
android:exported="false" />
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/ch/sdp/vibester/GreetingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ class GreetingActivity : AppCompatActivity() {

textViewActivity.text = "Hello $name!"


}
}
119 changes: 119 additions & 0 deletions app/src/main/java/ch/sdp/vibester/Register.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package ch.sdp.vibester

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import ch.sdp.vibester.auth.FireBaseAuthenticator
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult

class Register : AppCompatActivity() {

// private lateinit var googleSignInClient: GoogleSignInClient

private lateinit var authenticator: FireBaseAuthenticator

private lateinit var email: TextView


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_google_log_in)

// val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding @fixme or explain in the comment why stop using google sign in to remind us later in case

// .requestEmail()
// .build()

// googleSignInClient = GoogleSignIn.getClient(this, gso)

// Initialize Firebase Auth
authenticator = FireBaseAuthenticator()

val btCreateAcc = findViewById<Button>(R.id.createAcc)
val btLogIn = findViewById<Button>(R.id.logIn)
// val googleSignIn = findViewById<Button>(R.id.googleBtn)


val username = findViewById<EditText>(R.id.username)
val password = findViewById<EditText>(R.id.password)
email = findViewById<TextView>(R.id.email)

btCreateAcc.setOnClickListener {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

createAccount(username.text.toString(), password.text.toString())
}

btLogIn.setOnClickListener {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

signIn(username.text.toString(), password.text.toString())
}

// googleSignIn.setOnClickListener {
// signInGoogle()
// }


}

public override fun onStart() {
super.onStart()
val currentUser = authenticator.auth.currentUser
if(currentUser != null){
reload();
}
}

// public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// super.onActivityResult(requestCode, resultCode, data)
// updateUI(authenticator.googleActivityResult(requestCode, resultCode, data));
// }

// private fun signInGoogle() {
// val intent = googleSignInClient.signInIntent
// startActivityForResult(intent, 1000)
// }

private fun createAccount(email: String, password: String) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

authenticator.createAccount(email, password)
.addOnCompleteListener(this) { task ->
onCompleteAuthentication(task)
}
}

private fun signIn(email: String, password: String) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

authenticator.signIn(email, password)
.addOnCompleteListener(this) { task ->
onCompleteAuthentication(task)
}
}

private fun onCompleteAuthentication(task: Task<AuthResult>) {
if (task.isSuccessful) {
Toast.makeText(
baseContext, "You have logged in successfully",
Toast.LENGTH_SHORT
).show()
val user = authenticator.auth.currentUser
if (user != null) {
updateUI(user.email)
}
} else {
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
updateUI("Authentication error")
}
}

private fun reload() {

}

private fun updateUI(emailText: String?) {
email.text = emailText
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/ch/sdp/vibester/WelcomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class WelcomeScreen : AppCompatActivity() {
}

fun switchToSettings(view: View) { //FILLER INTENT
sendDirectIntent(GameSetupScreen::class.java)
sendDirectIntent(Register::class.java)
}

/*
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.sdp.vibester.auth

import android.content.ContentValues
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class FireBaseAuthenticator() {


val auth: FirebaseAuth = Firebase.auth


fun signIn(email: String, password: String): Task<AuthResult> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

return auth.signInWithEmailAndPassword(email, password)
}

fun createAccount(email: String, password: String): Task<AuthResult> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

return auth.createUserWithEmailAndPassword(email, password)
}

// fun googleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): String? {
// return if(requestCode == 1000) {
// val task = GoogleSignIn.getSignedInAccountFromIntent(data)
// try {
// val account = task.getResult(ApiException::class.java)!!
// account.email
// } catch (e: ApiException) {
// "Authentication error"
// }
// } else {
// "Authentication error"
// }
// }

}
Loading