Skip to content

Commit

Permalink
[nunu/#43] feat: SplashActivity 자동로그인 구현
Browse files Browse the repository at this point in the history
- jwt Header에 담기 (Retrofit 공식문서 참고)
- 성공 시 MainActivity 이동
- 실패 시 LoginActivity 이동
  • Loading branch information
Ssamssamukja committed Jun 18, 2024
1 parent d52e7ee commit 7473c1e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivitySplashBinding
import retrofit2.Call

class SplashActivity :AppCompatActivity() {
private lateinit var binding : ActivitySplashBinding
Expand All @@ -13,10 +14,52 @@ class SplashActivity :AppCompatActivity() {
binding = ActivitySplashBinding.inflate(layoutInflater)
setContentView(binding.root)

// 자동 로그인 시도
// attemptAutoLogin()

android.os.Handler().postDelayed({
startActivity(Intent(this, MainActivity::class.java))
finish()
overridePendingTransition(0, 0)
}, 2000)
}

/* 자동 로그인 splash
private fun attemptAutoLogin() {
val token = loadJwtToken()
if (token.isNullOrEmpty()) {
navigateToLoginActivity()
} else {
RetrofitInstance.authApi.autoLogin("Bearer $token").enqueue(object : Callback<BaseResponse> {
override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
if (response.isSuccessful && response.body()?.code == 1000) {
navigateToMainActivity()
} else {
navigateToLoginActivity()
}
}
override fun onFailure(call: Call<BaseResponse>, t: Throwable) {
navigateToLoginActivity()
}
})
}
}
private fun loadJwtToken(): String? {
// SharedPreferences에서 JWT 토큰 로드
return getSharedPreferences("UserPrefs", MODE_PRIVATE).getString("jwt_token", null)
}
private fun navigateToMainActivity() {
startActivity(Intent(this, MainActivity::class.java)) // MainActivity로 이동
finish()
}
private fun navigateToLoginActivity() {
startActivity(Intent(this, LoginActivity::class.java)) //LoginActivity로 이동
finish()
}
*/
}

0 comments on commit 7473c1e

Please sign in to comment.