Files
forkmessager/client-mobile/core/security/TokenManager.kt
2026-04-14 01:41:51 +03:00

43 lines
1.2 KiB
Kotlin

package core.security
import android.content.Context
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class TokenManager @Inject constructor(context: Context) {
private val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
private val prefs = EncryptedSharedPreferences.create(
context,
"auth_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
fun saveToken(token: String, userId: String? = null) {
val editor = prefs.edit().putString("jwt_token", token)
if (userId != null) {
editor.putString("user_id", userId)
}
editor.apply()
}
fun getToken(): String? {
return prefs.getString("jwt_token", null)
}
fun getUserId(): String? {
return prefs.getString("user_id", null)
}
fun deleteToken() {
prefs.edit().remove("jwt_token").remove("user_id").apply()
}
}