generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 [Feat] : 추가 TokenInterceptor에 토큰 및 SharedPreference의 토큰
- Loading branch information
1 parent
0b4122c
commit 7c6898b
Showing
8 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.sopt.and.data | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import org.sopt.and.domain.SharedPreferenceManager | ||
|
||
class TokenInterceptor : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val token = SharedPreferenceManager.getAccessToken() | ||
val requestBuilder = chain.request().newBuilder() | ||
|
||
if (token != null) { | ||
requestBuilder.addHeader("token", token) | ||
} | ||
|
||
return chain.proceed(requestBuilder.build()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.sopt.and.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RequestUserLoginDto( | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("password") | ||
val password: String, | ||
) | ||
|
||
@Serializable | ||
data class ResponseUserLoginDto( | ||
// success와 failed의 다른 필드를 정의하기 위해 null로 설정 | ||
@SerialName("result") | ||
val result: ResponseLoginResultDto? = null, | ||
@SerialName("code") | ||
val code: String? = null | ||
) | ||
|
||
@Serializable | ||
data class ResponseLoginResultDto( | ||
@SerialName("token") | ||
val token: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 10 additions & 13 deletions
23
app/src/main/java/org/sopt/and/domain/SharedPreferenceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
package org.sopt.and.domain | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import org.sopt.and.Application | ||
|
||
object SharedPreferenceManager { | ||
private lateinit var sharedPreferences: SharedPreferences | ||
private const val PREF_NAME = "app_preferences" | ||
private val preferences = Application.appContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) | ||
|
||
fun initialize(context: Context) { | ||
// 이미 초기화된 경우에는 초기화를 건너뛰게 설정 | ||
if(::sharedPreferences.isInitialized){ | ||
return | ||
} | ||
sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE) | ||
fun saveToken(token: String) { | ||
preferences.edit().putString("token", token).apply() | ||
} | ||
|
||
fun saveUserId(userId: String) { | ||
sharedPreferences.edit().putString("user_id", userId).apply() | ||
fun saveId(userId: String) { | ||
preferences.edit().putString("userId", userId).apply() | ||
} | ||
|
||
fun getUserId(): String? { | ||
return sharedPreferences.getString("user_id", null) | ||
fun getAccessToken(): String? { | ||
return preferences.getString("token", null) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters