-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…면-로그인-생략하는-기능-구현 [AN] feat(#359): 앱 실행 시 token 유효하면 로그인 생략하는 기능 구현
- Loading branch information
Showing
3 changed files
with
81 additions
and
6 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
47 changes: 47 additions & 0 deletions
47
...udy/presentation/src/main/java/com/takseha/presentation/viewmodel/auth/SplashViewModel.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,47 @@ | ||
package com.takseha.presentation.viewmodel.auth | ||
|
||
import android.app.Application | ||
import android.util.Log | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import com.takseha.common.model.SPKey | ||
import com.takseha.common.util.SP | ||
import com.takseha.data.repository.auth.GitudyAuthRepository | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class SplashViewModel(application: Application) : AndroidViewModel(application) { | ||
private var gitudyAuthRepository: GitudyAuthRepository = GitudyAuthRepository() | ||
private val prefs = SP(getApplication()) | ||
|
||
private val bearerToken = "Bearer ${prefs.loadPref(SPKey.ACCESS_TOKEN, "0")} ${ | ||
prefs.loadPref(SPKey.REFRESH_TOKEN, "0") | ||
}" | ||
|
||
private var _availableTokenCheck = MutableLiveData<Boolean>() | ||
val availableTokenCheck : LiveData<Boolean> | ||
get() = _availableTokenCheck | ||
|
||
suspend fun checkAvailableToken() { | ||
val checkTokenResponse = gitudyAuthRepository.getUserInfo(bearerToken) | ||
Log.d("SplashViewModel", bearerToken) | ||
|
||
if (checkTokenResponse.isSuccessful) { | ||
val resCode = checkTokenResponse.body()!!.resCode | ||
val resMsg = checkTokenResponse.body()!!.resMsg | ||
|
||
if (resCode == 200 && resMsg == "OK") { | ||
_availableTokenCheck.value = true | ||
} else { | ||
_availableTokenCheck.value = false | ||
Log.e("SplashViewModel", "https status error: $resCode, $resMsg") | ||
} | ||
} else { | ||
Log.e( | ||
"SplashViewModel", | ||
"tokenResponse status: ${checkTokenResponse.code()}\ntokenResponse message: ${checkTokenResponse.message()}" | ||
) | ||
} | ||
} | ||
} |