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

Rework Authentication #180

Merged
merged 8 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,6 @@ class AuthenticationActivityTest {
onView(withId(R.id.email)).check(matches(withText("Password has to be at least 6 symbols")))
}

@Test
fun derinTest() {
val a = FireBaseAuthenticator()

a.googleActivityResult(-1, -1, null)

onView(withId(R.id.email)).check(matches(withText("TextView")))
}

@Test
fun ardaTest() {
val a = FireBaseAuthenticator()

a.googleActivityResult(1000, -1, null)

onView(withId(R.id.email)).check(matches(withText("TextView")))
}

@Test
fun logInCorrect() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.sdp.vibester.activity

import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.intent.Intents
Expand All @@ -9,6 +11,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import com.google.firebase.auth.FirebaseAuth
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
Expand All @@ -23,11 +26,8 @@ class WelcomeActivityTest {
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)


@get:Rule(order = 1)
val testRule = ActivityScenarioRule(
WelcomeActivity::class.java
)
val testRule = ActivityScenarioRule(WelcomeActivity::class.java)

@Before
fun setUp() {
Expand All @@ -46,7 +46,15 @@ class WelcomeActivityTest {
}

@Test
fun checkIntentOnProfile() {
fun checkIntentOnMyAccountLoggedOut() {
FirebaseAuth.getInstance().signOut()
onView(withId(R.id.welcome_profile)).perform(click())
intended(hasComponent(AuthenticationActivity::class.java.name))
}

@Test
fun checkIntentOnMyAccountLoggedIn() {
WelcomeActivity.setLoggedIn()
onView(withId(R.id.welcome_profile)).perform(click())
intended(hasComponent(ProfileActivity::class.java.name))
}
Expand All @@ -57,12 +65,6 @@ class WelcomeActivityTest {
intended(hasComponent(ScoreBoardActivity::class.java.name))
}

@Test
fun checkIntentOnSettings() {
onView(withId(R.id.welcome_settings)).perform(click())
intended(hasComponent(AuthenticationActivity::class.java.name))
}

@Test
fun checkIntentOnDownload() {
onView(withId(R.id.welcome_download)).perform(click())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.helper.IntentSwitcher
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
import com.google.firebase.auth.FirebaseAuth
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand All @@ -33,14 +35,15 @@ class AuthenticationActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
supportActionBar?.hide()
setContentView(R.layout.activity_google_log_in)
setContentView(R.layout.activity_authentication)

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

googleSignInClient = GoogleSignIn.getClient(this, gso)


val btCreateAcc = findViewById<Button>(R.id.createAcc)
val btLogIn = findViewById<Button>(R.id.logIn)
val googleSignIn = findViewById<Button>(R.id.googleBtn)
Expand All @@ -49,6 +52,8 @@ class AuthenticationActivity : AppCompatActivity() {
val password = findViewById<EditText>(R.id.password)
email = findViewById(R.id.email)



btCreateAcc.setOnClickListener {
authenticate(username.text.toString(), password.text.toString(), true)
}
Expand Down
52 changes: 42 additions & 10 deletions app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package ch.sdp.vibester.activity

import android.graphics.Bitmap
import android.os.Bundle
import android.text.InputType
import android.util.Log
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import ch.sdp.vibester.R
import ch.sdp.vibester.api.BitmapGetterApi
import ch.sdp.vibester.model.UserSharedPref
import ch.sdp.vibester.database.UsersRepo
import ch.sdp.vibester.helper.IntentSwitcher
import ch.sdp.vibester.profile.UserProfile
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.firebase.auth.FirebaseAuth
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -35,11 +42,15 @@ class ProfileActivity : AppCompatActivity() {

setContentView(R.layout.activity_profile)

setupProfile(UserSharedPref.getUser(this))
setupProfile(UserSharedPref.getUser(applicationContext))

val editUsername = findViewById<Button>(R.id.editUser)
val editHandle = findViewById<Button>(R.id.editHandle)

val logoutbutton = findViewById<Button>(R.id.logout)

val retToMain = findViewById<FloatingActionButton>(R.id.profile_returnToMain)

editUsername.setOnClickListener {
showGeneralDialog(R.id.username, "username")
}
Expand All @@ -48,6 +59,15 @@ class ProfileActivity : AppCompatActivity() {
showGeneralDialog(R.id.handle, "handle")
}

retToMain.setOnClickListener{
IntentSwitcher.switchBackToWelcome(this)
}

logoutbutton.setOnClickListener{
FirebaseAuth.getInstance().signOut()
IntentSwitcher.switchBackToWelcome(this)
}

}

/**
Expand Down Expand Up @@ -105,24 +125,36 @@ class ProfileActivity : AppCompatActivity() {


private fun setupProfile(user: UserProfile){
findViewById<TextView>(R.id.handle).text = user.handle
findViewById<TextView>(R.id.username).text = user.username
findViewById<TextView>(R.id.totalGames).text = user.totalGames.toString()
findViewById<TextView>(R.id.correctSongs).text = user.correctSongs.toString()
findViewById<TextView>(R.id.bestScore).text = user.bestScore.toString()
findViewById<TextView>(R.id.ranking).text = user.ranking.toString()

// Currently assuming that empty username means no user !
if (user.username != ""){
findViewById<TextView>(R.id.username).text = user.username
if (user.handle != ""){
findViewById<TextView>(R.id.handle).text = user.handle
}
findViewById<TextView>(R.id.totalGames).text = user.totalGames.toString()
findViewById<TextView>(R.id.correctSongs).text = user.correctSongs.toString()
findViewById<TextView>(R.id.bestScore).text = user.bestScore.toString()
findViewById<TextView>(R.id.ranking).text = user.ranking.toString()
}
CoroutineScope(Dispatchers.Main).launch {
val task = async(Dispatchers.IO) {
try {
val bit = BitmapGetterApi.download("https://" + user.image)
bit.get()
Log.e(getString(R.string.log_tag),user.image)
val bit = BitmapGetterApi.download("https://raw.githubusercontent.com/Ashwinvalento/cartoon-avatar/master/lib/images/male/45.png")
Copy link
Collaborator

Choose a reason for hiding this comment

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

something to think about...

There are three options about profile images:

  1. We save the default image link in every profile in firebase.
  2. We save an image in our app
  3. We upload the picture from github every time (as you do)

We use profile picture in scoreboard, user search and in a profile account. If we take scoreboard, there might be a lot of profiles without the image, maybe it would be better if each of the profile has a default image, so we dont do extra check and upload image your way. Or it would be better to have an image locally, since it is same and we dont upload your way every time.

Copy link
Owner Author

Choose a reason for hiding this comment

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

yes we will see and try to solve this issue. Currently this is just a random image to test the display. I will rework it in part 2. There was issue with the database and account code that I will solve in part 2.

bit.get(10, TimeUnit.SECONDS)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the user image will be displayed during 10 seconds? isnt it a lot?

Copy link
Owner Author

Choose a reason for hiding this comment

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

No the 10 sec is the timeout to get the image. Indeed it is a magic number for the moment, but I may set it a global var for all async timeout and should choose the value later

Copy link
Collaborator

Choose a reason for hiding this comment

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

got you!

}catch (e: Exception){
null
}
}
val bm = task.await()
findViewById<ImageView>(R.id.avatar).setImageBitmap(bm)

if(bm != null){
val avatar = findViewById<ImageView>(R.id.avatar)
avatar.setImageBitmap(Bitmap.createScaledBitmap(bm, 1000,1000, false))
}
}

}
}

36 changes: 23 additions & 13 deletions app/src/main/java/ch/sdp/vibester/activity/WelcomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@ import android.view.Window.FEATURE_NO_TITLE
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.auth.FireBaseAuthenticator
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import ch.sdp.vibester.model.UserSharedPref

class WelcomeActivity : AppCompatActivity() {

// For test purpose
companion object{
var testLoggedIn: Boolean = false

fun setLoggedIn(){
testLoggedIn = true
}

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(FEATURE_NO_TITLE)
supportActionBar?.hide()
setContentView(R.layout.activity_welcome_screen)

val tv = findViewById<TextView>(R.id.user_status)
val username = UserSharedPref.getUser(this).username
if(username != "")
val userStatusTextValue = findViewById<TextView>(R.id.user_status)
if(FireBaseAuthenticator.isLoggedIn() || testLoggedIn)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we are pushing to the main branch, I believe the code should be as clean as possible. So I am not sure whether the testing variables hsould be in main

Copy link
Owner Author

Choose a reason for hiding this comment

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

this is testing var for cirrus, this is necessary (trick to allow unit test to test the below code, otherwise impossible)

Copy link
Collaborator

Choose a reason for hiding this comment

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

ohhh, i seee

{
tv.text = "User: " + username
userStatusTextValue.text = "User: " + FireBaseAuthenticator.getCurrentUserMail()
}

val currentEmail = UserSharedPref.getUser(this).email
if (currentEmail != null){
UserSharedPref.userReset(this, currentEmail)
}

}

private fun sendDirectIntent(arg: Class<*>?) {
Expand All @@ -40,17 +50,17 @@ class WelcomeActivity : AppCompatActivity() {
}

fun switchToProfile(view: View) {
sendDirectIntent(ProfileActivity::class.java)
if (FireBaseAuthenticator.isLoggedIn() || testLoggedIn){
sendDirectIntent(ProfileActivity::class.java)
}else{
sendDirectIntent(AuthenticationActivity::class.java)
}
}

fun switchToScoreboard(view: View) {
sendDirectIntent(ScoreBoardActivity::class.java)
}

fun switchToSettings(view: View) {
sendDirectIntent(AuthenticationActivity::class.java)
}

fun switchToDownload(view: View) {
sendDirectIntent(DownloadActivity::class.java)
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ import dagger.hilt.android.scopes.ServiceScoped
import javax.inject.Inject

class FireBaseAuthenticator @Inject constructor() {
companion object{

/**
* API: return true if firebase authentication is logged in
*/
fun isLoggedIn(): Boolean {
return !(FirebaseAuth.getInstance().currentUser == null)
}

/**
* API: return the mail of the user if logged in otherwise empty string
*/
fun getCurrentUserMail(): String {
if (isLoggedIn()) {
return FirebaseAuth.getInstance().currentUser!!.email.toString()
} else {
return ""
}
}

}

private val auth: FirebaseAuth = Firebase.auth

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/ch/sdp/vibester/database/UsersRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ class UsersRepo @Inject constructor() {
if(dbContents.email == email) {
callback(dbContents)
break
} else {
callback(UserProfile())
}
}
}
Expand Down
Loading