I want to know how can I access my token stored in Shared Preferences and pass it in the url when required.
RetrofitClient.kt file :
class OAuthInterceptor(private val tokenType: String, private val access_token: String): Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
request = request.newBuilder().header("Authorization", "$tokenType $access_token").build()
return chain.proceed(request)
}
}
val client = OkHttpClient.Builder()
.addInterceptor(OAuthInterceptor("Bearer", access_token))
.build()
val retrofit = Retrofit.Builder()
.baseUrl("___________________")
.client(client)
.build()
object RetrofitClient {
private const val BASE_URL = "_______"
private val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.method(original.method, original.body)
val request = requestBuilder.build()
chain.proceed(request)
}.build()
val instance: MyApi by lazy{
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
retrofit.create(MyApi::class.java)
}
}
Below is my SharedPrefManager file :
class SharedPrefManager private constructor(private val mCtx: Context) {
val user: User
get() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
return User(
sharedPreferences.getString(user.access_token, ""),
sharedPreferences.getString(user.user_id.toString(), "" ),
sharedPreferences.getString(user.user_name, ""),
sharedPreferences.getString(user.status, ""),
sharedPreferences.getString(user.role_id,""),
sharedPreferences.getString(user.phone, "")
)
}
fun saveUser(user: User) {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("access_token", user.access_token)
editor.putString("id", user.user_id)
editor.putString("username", user.user_name)
editor.putString("status", user.status)
editor.putString("role_id", user.role_id)
editor.putString("phone", user.phone)
editor.apply()
}
fun clear() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()
}
companion object {
private const val SHARED_PREF = "my_shared_pref"
private var mInstance: SharedPrefManager? = null
@Synchronized
fun getInstance(mCtx: Context): SharedPrefManager {
if (mInstance == null) {
mInstance = SharedPrefManager(mCtx)
}
return mInstance as SharedPrefManager
}
}
}
CodePudding user response:
You should use the name of the save preference when retrieving them.
Try to change your SharedPrefManager
get
method like this:
get() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
return User(
sharedPreferences.getString("access_token", ""),
sharedPreferences.getString("id", "" ),
sharedPreferences.getString("username", ""),
sharedPreferences.getString("status", ""),
sharedPreferences.getString("role_id",""),
sharedPreferences.getString("phone", "")
)
}