-
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.
Merge pull request #36 from EAT-SSU/34-add-splash-screen
34 add splash screen
- Loading branch information
Showing
7 changed files
with
94 additions
and
109 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
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/eatssu/android/data/RefreshTokenService.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.eatssu.android.data | ||
|
||
import RetrofitImpl.retrofit | ||
import com.eatssu.android.App | ||
import com.eatssu.android.data.service.UserService | ||
|
||
object RefreshTokenService { | ||
|
||
private val userService = retrofit.create(UserService::class.java) | ||
|
||
fun refreshToken(): String { | ||
val res = userService.getNewToken().execute() | ||
if (res.isSuccessful) { | ||
val newAccessToken = res.body()?.accessToken ?: "" | ||
val newRefreshToken = res.body()?.refreshToken ?: "" | ||
if (newAccessToken.isNotBlank()) { | ||
App.token_prefs.accessToken = newAccessToken | ||
App.token_prefs.refreshToken = newRefreshToken | ||
return newAccessToken | ||
} | ||
} | ||
throw IllegalStateException("ํ ํฐ ์ฌ๋ฐ๊ธ ์คํจ") | ||
} | ||
} |
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
113 changes: 18 additions & 95 deletions
113
app/src/main/java/com/eatssu/android/data/TokenAuthenticator.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,102 +1,25 @@ | ||
package com.eatssu.android.data | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.runBlocking | ||
import android.util.Log | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import retrofit2.Retrofit | ||
|
||
//class TokenAuthenticator constructor( | ||
// val context: Context, | ||
// private val refreshToken: String, | ||
//) : Authenticator { | ||
// | ||
// companion object { | ||
// private val TAG = TokenAuthenticator::class.java.simpleName | ||
// } | ||
// | ||
// override fun authenticate(route: Route?, response: Response): Request? { | ||
// | ||
// if (response.code == 401) { | ||
// | ||
// val refreshToken = CommonHelper.getRefreshToken(sharedPref) | ||
// val getNewDeviceToken = GlobalScope.async(Dispatchers.Default) { | ||
// getNewDeviceToken(refreshToken) | ||
// } | ||
// | ||
// val token = runBlocking { | ||
// getNewDeviceToken.await() | ||
// } | ||
// if(token != null) { | ||
// return getRequest(response, token) | ||
// } | ||
// } | ||
// return null | ||
// } | ||
// | ||
// private suspend inline fun getNewDeviceToken(token: String): String? { | ||
// return GlobalScope.async(Dispatchers.Default) { | ||
// callApiNewDeviceToken(token) | ||
// }.await() | ||
// } | ||
// | ||
// | ||
// private suspend inline fun callApiNewDeviceToken(token: String) : String? = suspendCoroutine { continuation -> | ||
// createWebService<Api>() | ||
// .refreshToken(RefreshToken(token)) | ||
// .with(rx) | ||
// .response(object : ApiCallback<Token>{ | ||
// override fun success(data: Token?) { | ||
// if(data != null) { | ||
// CommonHelper.saveTokenInfo(sharedPref, data) | ||
// continuation.resume(data.accessToken) | ||
// } else { | ||
// continuation.resume(null) | ||
// } | ||
// } | ||
// | ||
// override fun error(statusCode: Int, message: String?) { | ||
// continuation.resume(null) | ||
// } | ||
// }) | ||
// | ||
// return@suspendCoroutine | ||
// } | ||
// | ||
// private val okHttp = OkHttpClient.Builder() | ||
// .connectTimeout(TIMEOUT_LIMIT, TimeUnit.SECONDS) | ||
// .readTimeout(TIMEOUT_LIMIT, TimeUnit.SECONDS) | ||
// .writeTimeout(TIMEOUT_LIMIT, TimeUnit.SECONDS) | ||
// .addInterceptor(HttpLoggingInterceptor().apply { | ||
// level = if (BuildConfig.DEBUG) { | ||
// HttpLoggingInterceptor.Level.BODY | ||
// } else { | ||
// HttpLoggingInterceptor.Level.NONE | ||
// } | ||
// }) | ||
// .build() | ||
// | ||
// private inline fun <reified T> createWebService(): T { | ||
// val retrofit = Retrofit.Builder() | ||
// .baseUrl(BuildConfig.SERVER_URL) | ||
// .client(okHttp) | ||
// .addConverterFactory(GsonConverterFactory.create( | ||
// GsonBuilder().serializeNulls().create() | ||
// )) | ||
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build() | ||
// return retrofit.create(T::class.java) | ||
// } | ||
// | ||
// private fun getRequest(response: Response, token: String): Request { | ||
// return response.request | ||
// .newBuilder() | ||
// .removeHeader("Authorization") | ||
// .addHeader("Authorization", "Bearer $token") | ||
// .build() | ||
// } | ||
//} | ||
class TokenAuthenticator: Authenticator { | ||
override fun authenticate(route: Route?, response: Response): Request? { | ||
Log.i("Authenticator", response.toString()) | ||
Log.i("Authenticator", "ํ ํฐ ์ฌ๋ฐ๊ธ ์๋") | ||
return try { | ||
val newAccessToken = RefreshTokenService.refreshToken() | ||
Log.i("Authenticator", "ํ ํฐ ์ฌ๋ฐ๊ธ ์ฑ๊ณต : $newAccessToken") | ||
response.request.newBuilder() | ||
.removeHeader("Bearer").apply { | ||
addHeader("Bearer", newAccessToken) | ||
}.build() // ํ ํฐ ์ฌ๋ฐ๊ธ์ด ์ฑ๊ณตํ๋ค๋ฉด, ๊ธฐ์กด ํค๋๋ฅผ ์ง์ฐ๊ณ , ์๋ก์ด ํด๋๋ฅผ ๋จ๋ค. | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
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
File renamed without changes.