Skip to content

Commit

Permalink
[feat/#8] SharedPreferences로 토큰 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
hwidung committed Nov 12, 2024
1 parent 5d1a2b7 commit d032403
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.sopt.and.data.repository

import android.content.Context
import android.content.SharedPreferences
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideSharedPreferencesHelper(@ApplicationContext context: Context): SharedPreferencesHelper {
return SharedPreferencesHelper(context)
}
}


class SharedPreferencesHelper(context: Context) {
private val prefs: SharedPreferences =
context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)

fun saveToken(token: String) {
prefs.edit().putString("auth_token", token).apply()
}

fun getToken(): String? {
return prefs.getString("auth_token", null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
Expand All @@ -46,6 +47,7 @@ fun SignInScreen(
signInViewModel: SignInViewModel = hiltViewModel(),
navController: NavHostController
) {
val context = LocalContext.current
val signInResult by signInViewModel.loginResult.observeAsState()

var isPasswordVisible by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -128,7 +130,6 @@ fun SignInScreen(
)
signInResult?.let { result ->
if (result.isSuccess) {
// 토큰 받아와서 저장
coroutineScope.launch {
snackbarHostState.showSnackbar("로그인 성공!")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sopt.and.presentation.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -12,11 +13,13 @@ import kotlinx.coroutines.launch
import org.sopt.and.data.dto.RequestLoginData
import org.sopt.and.data.dto.ResponseLogin
import org.sopt.and.data.repository.Auth.LoginRepository
import org.sopt.and.data.repository.SharedPreferencesHelper
import javax.inject.Inject

@HiltViewModel
class SignInViewModel @Inject constructor(
private val loginRepository: LoginRepository
private val loginRepository: LoginRepository,
private val sharedPreferencesHelper: SharedPreferencesHelper
) : ViewModel() {

private val _loginResult = MutableLiveData<Result<ResponseLogin>>()
Expand All @@ -26,10 +29,19 @@ class SignInViewModel @Inject constructor(
viewModelScope.launch {
val loginRequest = RequestLoginData(username, password)
val result = loginRepository.postLogin(loginRequest)
result.onSuccess {
val token = it.result.token
saveToken(token)
Log.d("SignInResultViewModel", "토큰이 저장됐을까.. $token")
}
_loginResult.postValue(result)
}
}

fun saveToken(token: String) {
sharedPreferencesHelper.saveToken(token)
}

private var _username by mutableStateOf("")
val username: String
get() = _username
Expand Down

0 comments on commit d032403

Please sign in to comment.