Skip to content

Instantly share code, notes, and snippets.

@adit-gudhel
Created November 1, 2020 16:53
Show Gist options
  • Save adit-gudhel/cac63d5145f5ab2451a9ef623c0e5a25 to your computer and use it in GitHub Desktop.
Save adit-gudhel/cac63d5145f5ab2451a9ef623c0e5a25 to your computer and use it in GitHub Desktop.
com.gudhel.loginoauth.utils.SessionManager.kt
/**
* Session manager untuk menyimpan dan mengambil session dari SharedPreferences
*/
class SessionManager(context: Context) {
private var prefs: SharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE)
companion object {
const val ACCESS_TOKEN = "access_token"
}
/**
* Fungsi simpan access_token
*/
fun saveAccessToken(token: String) {
val editor = prefs.edit()
editor.putString(ACCESS_TOKEN, token)
.apply()
}
/**
* Fungsi get access_token
*/
fun fetchAccessToken(): String? {
return prefs.getString(ACCESS_TOKEN, null)
}
/**
* Fungsi delete access_token / clear shared preferences
*/
fun deleteAccessToken() {
val editor = prefs.edit()
editor.clear()
.apply()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment