diff --git a/Aos/.idea/deploymentTargetDropDown.xml b/Aos/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 2830ba7..0000000 --- a/Aos/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt index e0923c0..9740861 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt @@ -5,12 +5,29 @@ import android.content.Context import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.preferencesDataStore +import com.avengers.nibobnebob.BuildConfig import com.avengers.nibobnebob.presentation.util.Constants.APP_NAME +import com.navercorp.nid.NaverIdLoginSDK import dagger.hilt.android.HiltAndroidApp @HiltAndroidApp class App : Application(){ + + override fun onCreate() { + super.onCreate() + initializeNaverSDK() + } + companion object{ val Context.dataStore: DataStore by preferencesDataStore(name = APP_NAME) } + + private fun initializeNaverSDK(){ + NaverIdLoginSDK.initialize( + applicationContext, + BuildConfig.NAVER_LOGIN_CLIENT_ID, + BuildConfig.NAVER_LOGIN_CLIENT_SECRET, + APP_NAME) + NaverIdLoginSDK.showDevelopersLog(true) + } } \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/config/AccessTokenInterceptor.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/config/AccessTokenInterceptor.kt index 3d7f144..8e4e324 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/config/AccessTokenInterceptor.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/config/AccessTokenInterceptor.kt @@ -24,8 +24,8 @@ class AccessTokenInterceptor @Inject constructor(private val dataStoreManager: D } Log.d("토큰 테스트",accessToken.toString()) val builder: Request.Builder = chain.request().newBuilder() - accessToken.let { - builder.addHeader(AUTHORIZATION,"$BEARER $accessToken") + accessToken?.takeIf { it.isNotEmpty() }?.let { + builder.addHeader(AUTHORIZATION, "$BEARER $it") } return chain.proceed(builder.build()) } diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/data/model/BaseState.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/data/model/BaseState.kt index c8f30f9..eb6531f 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/data/model/BaseState.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/data/model/BaseState.kt @@ -6,7 +6,9 @@ import retrofit2.Response enum class StatusCode{ EMPTY, ERROR, - EXCEPTION + EXCEPTION, + ERROR_AUTH, + ERROR_NONE } sealed class BaseState { @@ -23,7 +25,11 @@ suspend fun runRemote(block: suspend () -> Response): BaseState { } ?: BaseState.Error(StatusCode.EMPTY, "응답이 비어있습니다") } else { val errorData = Gson().fromJson(response.errorBody()?.string(), BaseState.Error::class.java) - BaseState.Error(StatusCode.ERROR, errorData.message) + when(response.code()){ + 401 -> BaseState.Error(StatusCode.ERROR_AUTH, errorData.message) + 404 -> BaseState.Error(StatusCode.ERROR_NONE, errorData.message) + else -> BaseState.Error(StatusCode.ERROR, errorData.message) + } } } catch (e: Exception) { BaseState.Error(StatusCode.EXCEPTION, e.message.toString()) diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/data/remote/IntroApi.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/data/remote/IntroApi.kt index ab23c9f..47643db 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/data/remote/IntroApi.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/data/remote/IntroApi.kt @@ -5,6 +5,7 @@ import com.avengers.nibobnebob.data.model.response.BaseResponse import com.avengers.nibobnebob.data.model.response.NaverLoginResponse import retrofit2.Response import retrofit2.http.Body +import retrofit2.http.Header import retrofit2.http.POST interface IntroApi { @@ -15,5 +16,7 @@ interface IntroApi { ): Response @POST("api/auth/social-login") - suspend fun loginNaver(): Response> + suspend fun loginNaver( + @Header("Authorization")token : String + ): Response> } \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepository.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepository.kt index 53c9727..24bee85 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepository.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepository.kt @@ -12,5 +12,7 @@ interface IntroRepository { body: DetailSignupRequest ): Flow> - fun loginNaver(): Flow>> + fun loginNaver( + token: String + ): Flow>> } \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepositoryImpl.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepositoryImpl.kt index 632b873..8189e99 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepositoryImpl.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/data/repository/IntroRepositoryImpl.kt @@ -6,6 +6,7 @@ import com.avengers.nibobnebob.data.model.response.BaseResponse import com.avengers.nibobnebob.data.model.response.NaverLoginResponse import com.avengers.nibobnebob.data.model.runRemote import com.avengers.nibobnebob.data.remote.IntroApi +import com.avengers.nibobnebob.presentation.util.Constants.ACCESS import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import javax.inject.Inject @@ -19,8 +20,8 @@ class IntroRepositoryImpl @Inject constructor( emit(result) } - override fun loginNaver(): Flow>> = flow { - val result = runRemote { api.loginNaver() } + override fun loginNaver(token : String): Flow>> = flow { + val result = runRemote { api.loginNaver("$ACCESS $token") } emit(result) } } \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/Info.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/Info.kt deleted file mode 100644 index 65758e3..0000000 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/Info.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.avengers.nibobnebob.presentation.ui.intro.login - -data class CommonRequest( - val email : String ="", - val password : String ="" -) \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginFragment.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginFragment.kt index 5cf555e..ada8e9e 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginFragment.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginFragment.kt @@ -16,7 +16,10 @@ import com.avengers.nibobnebob.presentation.ui.intro.IntroActivity import com.avengers.nibobnebob.presentation.ui.intro.IntroViewModel import com.avengers.nibobnebob.presentation.ui.main.MainActivity import com.navercorp.nid.NaverIdLoginSDK +import com.navercorp.nid.oauth.NidOAuthLogin import com.navercorp.nid.oauth.OAuthLoginCallback +import com.navercorp.nid.profile.NidProfileCallback +import com.navercorp.nid.profile.data.NidProfileResponse import dagger.hilt.android.AndroidEntryPoint @@ -31,7 +34,6 @@ class LoginFragment : BaseFragment(R.layout.fragment_login override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.vm = viewModel - naverInitialize() initEventObserver() binding.btnNaver.setOnClickListener { @@ -53,14 +55,6 @@ class LoginFragment : BaseFragment(R.layout.fragment_login } } - private fun naverInitialize(){ - NaverIdLoginSDK.initialize( - requireContext(), - BuildConfig.NAVER_LOGIN_CLIENT_ID, - BuildConfig.NAVER_LOGIN_CLIENT_SECRET, TAG) - NaverIdLoginSDK.showDevelopersLog(true) - } - private fun naverLogin(){ val oAuthLoginCallback = object : OAuthLoginCallback{ override fun onError(errorCode: Int, message: String) { @@ -75,20 +69,36 @@ class LoginFragment : BaseFragment(R.layout.fragment_login override fun onSuccess() { val token = NaverIdLoginSDK.getAccessToken().toString() - viewModel.loginNaver(token) + NidOAuthLogin().callProfileApi(object : NidProfileCallback{ + override fun onError(errorCode: Int, message: String) { + onFailure(errorCode, message) + } + override fun onFailure(httpStatus: Int, message: String) { + val errorCode = NaverIdLoginSDK.getLastErrorCode().code + val errorDescription = NaverIdLoginSDK.getLastErrorDescription() + Log.d(TAG,"errorCode:$errorCode, errorDesc:$errorDescription") + } + + override fun onSuccess(result: NidProfileResponse) { + viewModel.naverEmail.value = result.profile?.email.toString() + viewModel.loginNaver(token) + } + }) } } NaverIdLoginSDK.authenticate(requireContext(), oAuthLoginCallback) } private fun NavController.toDetailSignup(){ - val action = LoginFragmentDirections.actionLoginFragmentToDetailSignupFragment() + val action = LoginFragmentDirections.actionLoginFragmentToDetailSignupFragment( + email = viewModel.naverEmail.value + ) this.navigate(action) } private fun NavController.toMainActivity(){ val intent = Intent(context, MainActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(intent) - (activity as IntroActivity).finish() } } \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginViewModel.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginViewModel.kt index fe2b3b0..b96cf9d 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginViewModel.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/LoginViewModel.kt @@ -1,10 +1,13 @@ package com.avengers.nibobnebob.presentation.ui.intro.login +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.avengers.nibobnebob.app.DataStoreManager import com.avengers.nibobnebob.data.model.BaseState +import com.avengers.nibobnebob.data.model.StatusCode import com.avengers.nibobnebob.data.repository.IntroRepository +import com.avengers.nibobnebob.presentation.ui.intro.login.model.UiLoginData import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -32,12 +35,13 @@ class LoginViewModel @Inject constructor( private val _events = MutableSharedFlow() val events = _events.asSharedFlow() - private val _uiState = MutableStateFlow(CommonRequest()) + private val _uiState = MutableStateFlow(UiLoginData()) val uiState = _uiState.asStateFlow() val email = MutableStateFlow("") val password = MutableStateFlow("") val autoLogin = MutableStateFlow(false) + val naverEmail = MutableStateFlow("") init { observeEmail() @@ -70,26 +74,22 @@ class LoginViewModel @Inject constructor( fun loginNaver(token : String){ viewModelScope.launch { - dataStoreManager.putAccessToken(token) - introRepository.loginNaver().onEach { - when(it){ + introRepository.loginNaver(token).onEach { state -> + when(state){ is BaseState.Success -> { dataStoreManager.putAutoLogin(true) - dataStoreManager.putAccessToken(it.data.body.accessToken.toString()) - dataStoreManager.putRefreshToken(it.data.body.refreshToken.toString()) - + dataStoreManager.putAccessToken(state.data.body.accessToken.toString()) + dataStoreManager.putRefreshToken(state.data.body.refreshToken.toString()) _events.emit(LoginEvent.NavigateToMain) } is BaseState.Error -> { - _events.emit(LoginEvent.NavigateToDetailSignup) -// when(it.statusCode){ -// 401 -> { -// Log.d(TAG,"401이 뜰일이 있나..?") -// } -// 404 -> { -// _events.emit(LoginEvent.NavigateToDetailSignup) -// } -// } + when(state.statusCode){ + StatusCode.ERROR_AUTH-> {Log.d(TAG,"토큰 오류")} + StatusCode.ERROR_NONE ->{ _events.emit(LoginEvent.NavigateToDetailSignup)} + else ->{ + Log.d(TAG,"오류 Exception") + } + } } } }.launchIn(viewModelScope) diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/model/UiLoginData.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/model/UiLoginData.kt new file mode 100644 index 0000000..c57843a --- /dev/null +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/intro/login/model/UiLoginData.kt @@ -0,0 +1,6 @@ +package com.avengers.nibobnebob.presentation.ui.intro.login.model + +data class UiLoginData( + val email : String ="", + val password : String ="" +) \ No newline at end of file diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashActivity.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashActivity.kt index 2f627d3..70e24c9 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashActivity.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashActivity.kt @@ -29,20 +29,20 @@ class SplashActivity : BaseActivity(ActivitySplashBinding repeatOnStarted { viewModel.events.collect { when (it) { - is SplashViewModel.NavigationEvent.NavigateToIntro -> moveToIntroActivity() - is SplashViewModel.NavigationEvent.NavigateToMain -> moveToMainActivity() + is SplashUiEvent.NavigateToIntro -> toIntroActivity() + is SplashUiEvent.NavigateToMain -> toMainActivity() } } } } - private fun moveToMainActivity() { + private fun toMainActivity() { val intent = Intent(this, MainActivity::class.java) startActivity(intent) finish() } - private fun moveToIntroActivity() { + private fun toIntroActivity() { val intent = Intent(this, IntroActivity::class.java) startActivity(intent) finish() diff --git a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashViewModel.kt b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashViewModel.kt index 341c017..11ddf45 100644 --- a/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashViewModel.kt +++ b/Aos/app/src/main/java/com/avengers/nibobnebob/presentation/ui/splash/SplashViewModel.kt @@ -7,32 +7,31 @@ import com.avengers.nibobnebob.presentation.base.BaseActivityViewModel import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.launch import javax.inject.Inject +sealed class SplashUiEvent { + data object NavigateToMain : SplashUiEvent() + data object NavigateToIntro : SplashUiEvent() +} + @HiltViewModel class SplashViewModel @Inject constructor( private val networkManager: NetworkManager, private val dataStoreManager: DataStoreManager, ) : BaseActivityViewModel(networkManager) { - - sealed class NavigationEvent { - data object NavigateToMain : NavigationEvent() - data object NavigateToIntro : NavigationEvent() - } - - private val TAG = "SplashViewModelDebug" - private val _events = MutableSharedFlow() - val events: SharedFlow get() = _events + private val _events = MutableSharedFlow() + val events: SharedFlow = _events.asSharedFlow() fun getAutoLogin() { viewModelScope.launch { dataStoreManager.getAutoLogin().collect { autoLogin -> dataStoreManager.getAccessToken().collect { accessToken -> if (autoLogin == true && accessToken != "") { - _events.emit(NavigationEvent.NavigateToMain) + _events.emit(SplashUiEvent.NavigateToMain) } else { - _events.emit(NavigationEvent.NavigateToIntro) + _events.emit(SplashUiEvent.NavigateToIntro) } } }