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 21 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"
}
}
88 changes: 88 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,88 @@
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, 1000)
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
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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!"


}
}
136 changes: 136 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,136 @@
package ch.sdp.vibester

import android.content.ContentValues.TAG
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
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.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.FirebaseUser
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class Register : AppCompatActivity() {

private lateinit var googleSignInClient: GoogleSignInClient

private lateinit var authenticator: FireBaseAuthenticator


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

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()

googleSignInClient = GoogleSignIn.getClient(this, gso)

// Initialize Firebase Auth
authenticator = FireBaseAuthenticator(googleSignInClient)



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)
val 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(), email)
}

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(), email)
}

googleSignIn.setOnClickListener {
signInGoogle()
}


}

public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = authenticator.auth.currentUser
if(currentUser != null){
reload();
}
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val email = findViewById<TextView>(R.id.email)
authenticator.googleActivityResult(requestCode, resultCode, data, email);
}

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

private fun createAccount(email: String, password: String, emailText: TextView) {
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.

// [START create_user_with_email]
authenticator.createAccount(email, password)
.addOnCompleteListener(this) { task ->
onCompleteSignIn(task, emailText)
}
}

private fun signIn(email: String, password: String, emailText: TextView) {
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.

// [START sign_in_with_email]
authenticator.signIn(email, password)
.addOnCompleteListener(this) { task ->
onCompleteSignIn(task, emailText)
}
// [END sign_in_with_email]
}

private fun onCompleteSignIn(task: Task<AuthResult>, emailText: TextView) {
if (task.isSuccessful) {
Toast.makeText(
baseContext, "You have logged in successfully",
Toast.LENGTH_SHORT
).show()
val user = authenticator.auth.currentUser
updateUI(user, emailText)
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
updateUI(null, emailText)
}
}

private fun reload() {

}

private fun updateUI(user: FirebaseUser?, emailText: TextView) {
if (user != null) {
emailText.text = user.email
}
else {
emailText.text = "Authentication error"
}
}
}
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
Loading