Browse Source

Generated custom salt key on login and signup fragment and saved on shared prefs

Khubaib 1 tuần trước cách đây
mục cha
commit
bef6dd73a7

+ 10 - 0
app/src/main/java/com/fastest/pass/app/Utils.kt

@@ -1,6 +1,8 @@
 package com.fastest.pass.app
 
 import android.util.Base64
+import android.util.Log
+import com.fastest.pass.login.data.model.UserInfo
 import java.security.MessageDigest
 import javax.crypto.Cipher
 import javax.crypto.KeyGenerator
@@ -63,5 +65,13 @@ class Utils {
 
             return decryptedString
         }
+
+        fun customSaltKey(userinfo: UserInfo) : String {
+            val id = userinfo.id
+            val email = userinfo.email
+            val saltKey = "${id}_${email}"
+            val filterSaltKey = saltKey.replace("[@.]".toRegex(), "")
+            return filterSaltKey
+        }
     }
 }

+ 1 - 1
app/src/main/java/com/fastest/pass/browse/presentation/ui/components/BrowseScreen.kt

@@ -48,7 +48,7 @@ enum class ClickTypeItemBrowse {
     Note,
     Contact,
     Driver,
-    Passport
+    Passport,
 }
 
 @Composable

+ 15 - 4
app/src/main/java/com/fastest/pass/helpers/BasePreferenceHelper.kt

@@ -8,16 +8,18 @@ import com.google.gson.reflect.TypeToken
 class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
 
     fun clearAllData() {
-        removePreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
+        removePreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN)
         removePreference(context, KEY_FILENAME, KEY_BEARER_TOKEN)
+        removePreference(context, KEY_FILENAME, KEY_USERINFO)
+        removePreference(context, KEY_FILENAME, KEY_SALT_KEY)
     }
 
     fun setLoggedInState(state: Boolean) {
-        putBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN, state)
+        putBooleanPreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN, state)
     }
 
     fun getLoggedInState(): Boolean {
-        return getBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
+        return getBooleanPreference(context, KEY_FILENAME, KEY_IS_LOGGED_IN)
     }
 
     fun saveToken(token: String) {
@@ -41,10 +43,19 @@ class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
         )
     }
 
+    fun saveCustomSaltKey(saltKey: String) {
+        putStringPreference(context, KEY_FILENAME, KEY_SALT_KEY, saltKey)
+    }
+
+    fun getCustomSaltKey(): String? {
+        return getStringPreference(context, KEY_FILENAME, KEY_SALT_KEY)
+    }
+
     companion object {
         private const val KEY_FILENAME = "file_fastestpass"
-        private const val KEY_ISLOGGEDIN = "key_isloggedin"
+        private const val KEY_IS_LOGGED_IN = "key_isloggedin"
         private const val KEY_BEARER_TOKEN = "key_bearer_token"
         private const val KEY_USERINFO = "key_userinfo"
+        private const val KEY_SALT_KEY = "key_salt_key"
     }
 }

+ 0 - 7
app/src/main/java/com/fastest/pass/home/presentation/ui/fragment/HomeFragment.kt

@@ -57,13 +57,6 @@ class HomeFragment : BaseFragment() {
                                 .background(colorResource(id = R.color.home_background_color))
                                 .padding(paddingValues.calculateBottomPadding())
                         ) {
-                            val userinfo = basePreferenceHelper.getUserInfo()
-                            val id = userinfo?.id
-                            val email = userinfo?.email
-
-                            val saltKey = "$id$email"
-                            Log.d("saltKey", "saltKey = $saltKey")
-
                             HomeScreen { clickType ->
                                 when (clickType) {
                                     ClickType.GOTO_ADD_NEW_ITEMS -> {

+ 3 - 0
app/src/main/java/com/fastest/pass/login/presentation/ui/LoginFragment.kt

@@ -19,6 +19,7 @@ import com.fastest.pass.app.BaseFragment
 import com.fastest.pass.R
 import com.fastest.pass.app.GenericLoader
 import com.fastest.pass.app.MainActivity
+import com.fastest.pass.app.Utils
 import com.fastest.pass.helpers.BasePreferenceHelper
 import com.fastest.pass.login.presentation.ui.components.ClickType
 import com.fastest.pass.login.presentation.ui.components.LoginScreen
@@ -67,6 +68,8 @@ class LoginFragment : BaseFragment() {
                                     basePreferenceHelper.setLoggedInState(true)
                                     basePreferenceHelper.saveToken(it.token)
                                     basePreferenceHelper.saveUserInfo(it.userinfo)
+                                    val saltKey = Utils.customSaltKey(it.userinfo)
+                                    basePreferenceHelper.saveCustomSaltKey(saltKey)
                                     viewmodel.navigateTo(LoginRoute.OpenDashBoardScreen)
                                 }
                             }

+ 6 - 1
app/src/main/java/com/fastest/pass/signup/presentation/ui/SignUpFragment.kt

@@ -10,6 +10,7 @@ import androidx.fragment.app.activityViewModels
 import androidx.fragment.app.viewModels
 import com.fastest.pass.app.BaseFragment
 import com.fastest.pass.app.GenericLoader
+import com.fastest.pass.app.Utils
 import com.fastest.pass.helpers.BasePreferenceHelper
 import com.fastest.pass.login.presentation.viewmodels.LoginViewModel
 import com.fastest.pass.signup.presentation.ui.components.ClickType
@@ -61,7 +62,11 @@ class SignUpFragment : BaseFragment() {
                         if (it.status) {
                             basePreferenceHelper.setLoggedInState(true)
                             basePreferenceHelper.saveToken(it.data?.token ?: "")
-                            it.data?.userinfo?.let { it1 -> basePreferenceHelper.saveUserInfo(it1) }
+                            it.data?.userinfo?.let {
+                                it1 -> basePreferenceHelper.saveUserInfo(it1)
+                                val saltKey = Utils.customSaltKey(it1)
+                                basePreferenceHelper.saveCustomSaltKey(saltKey)
+                            }
                             viewmodel.navigateTo(SignUpRoute.OpenDashBoardScreen)
                         }
                     }