Skip to content

Commit

Permalink
Merge pull request #76 from PawWithU/feature/login
Browse files Browse the repository at this point in the history
feature/login: 자동로그인, 로그아웃 기능 구현
  • Loading branch information
kang9366 authored Feb 26, 2024
2 parents 32824f9 + 47d97ab commit 4cf46a5
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 153 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ android {

dependencies {
implementation(project(":feature:main"))
implementation(project(":feature:login"))
implementation(project(":feature:intermediator"))

implementation(libs.androidx.core.splashscreen)
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ buildscript {
mavenCentral()
}
dependencies {
// gradlew generateProjectDependencyGraph
classpath(libs.dependency.graph)
classpath(libs.google.service)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.kusitms.connectdog.core.util.AppMode
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import javax.inject.Inject

Expand All @@ -21,6 +23,7 @@ class DataStoreRepository @Inject constructor(
private object PreferenceKeys {
val accessToken = stringPreferencesKey("access_token")
val refreshToken = stringPreferencesKey("refresh_token")
val appMode = stringPreferencesKey("app_mode")
}

suspend fun saveAccessToken(accessToken: String) {
Expand All @@ -35,11 +38,25 @@ class DataStoreRepository @Inject constructor(
}
}

suspend fun saveAppMode(appMode: AppMode) {
context.dataStore.edit { preferences ->
preferences[PreferenceKeys.appMode] = appMode.name
}
}

val accessTokenFlow: Flow<String?> = context.dataStore.data
.map { preferences ->
preferences[PreferenceKeys.accessToken]
}

val appModeFlow: Flow<AppMode> = context.dataStore.data
.map { preferences ->
AppMode.valueOf(preferences[PreferenceKeys.appMode].toString())
}
.catch {
emit(AppMode.LOGIN)
}

val refreshTokenFlow: Flow<String?> = context.dataStore.data
.map { preferences ->
preferences[PreferenceKeys.accessToken]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.kusitms.connectdog.core.util

enum class Mode {
enum class AppMode {
LOGIN,
VOLUNTEER,
INTERMEDIATOR
}

enum class Type {
enum class UserType {
SOCIAL_VOLUNTEER,
NORMAL_VOLUNTEER,
INTERMEDIATOR
Expand Down
2 changes: 0 additions & 2 deletions feature/login/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ dependencies {

implementation(project(":core:designsystem"))
implementation(project(":core:util"))
implementation(project(":feature:intermediator"))
implementation(project(":feature:signup"))

implementation(libs.androidx.core.splashscreen)
implementation(project(mapOf("path" to ":core:data")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.kusitms.connectdog.core.util.Type
import com.kusitms.connectdog.core.util.UserType
import com.kusitms.connectdog.feature.login.screen.LoginRoute
import com.kusitms.connectdog.feature.login.screen.NormalLoginScreen
import com.kusitms.connectdog.signup.SignUpRoute

fun NavController.navigateLogin() {
navigate(LoginRoute.route)
}

fun NavController.navigateSignup(type: Type) {
navigate("${SignUpRoute.route}/$type")
fun NavController.onLogoutClick() {
navigate(LoginRoute.route) {
popUpTo(graph.id) {
inclusive = true
}
}
}

fun NavController.navigateNormalLogin(type: Type) {
navigate("${LoginRoute.normal_login}/$type")
fun NavController.navigateNormalLogin(userType: UserType) {
navigate("${LoginRoute.normal_login}/$userType")
}

fun NavGraphBuilder.loginNavGraph(
onBackClick: () -> Unit,
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToVolunteer: () -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToSignup: (UserType) -> Unit
) {
composable(route = LoginRoute.route) {
LoginRoute(
Expand All @@ -39,13 +38,13 @@ fun NavGraphBuilder.loginNavGraph(
route = "${LoginRoute.normal_login}/{type}",
arguments = listOf(
navArgument("type") {
type = NavType.EnumType(Type::class.java)
type = NavType.EnumType(UserType::class.java)
}
)
) {
NormalLoginScreen(
onBackClick = onBackClick,
type = it.arguments!!.getSerializable("type") as Type,
userType = it.arguments!!.getSerializable("type") as UserType,
onNavigateToVolunteerHome = onNavigateToVolunteer
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ import com.kusitms.connectdog.core.designsystem.component.ConnectDogNormalButton
import com.kusitms.connectdog.core.designsystem.theme.Gray2
import com.kusitms.connectdog.core.designsystem.theme.KAKAO
import com.kusitms.connectdog.core.designsystem.theme.NAVER
import com.kusitms.connectdog.core.util.Type
import com.kusitms.connectdog.core.util.UserType
import com.kusitms.connectdog.feature.login.R
import kotlinx.coroutines.launch

val pages = listOf("이동봉사자 회원", "이동봉사자 중개 회원")

@Composable
internal fun LoginRoute(
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToSignup: (UserType) -> Unit
) {
LoginScreen(
onNavigateToNormalLogin = onNavigateToNormalLogin,
Expand All @@ -57,8 +57,8 @@ internal fun LoginRoute(

@Composable
fun LoginScreen(
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToSignup: (UserType) -> Unit
) {
Column(
modifier = Modifier
Expand All @@ -84,8 +84,8 @@ fun LoginScreen(
@OptIn(ExperimentalPagerApi::class)
@Composable
fun SelectLoginType(
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToSignup: (UserType) -> Unit
) {
Surface {
Column() {
Expand Down Expand Up @@ -132,8 +132,8 @@ fun SelectLoginType(

@Composable
fun Volunteer(
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToSignup: (UserType) -> Unit
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down Expand Up @@ -176,17 +176,17 @@ fun Volunteer(
.fillMaxWidth()
.padding(horizontal = 20.dp),
content = stringResource(id = R.string.signup_with_connectdog),
onClick = { onNavigateToSignup(Type.NORMAL_VOLUNTEER) }
onClick = { onNavigateToSignup(UserType.NORMAL_VOLUNTEER) }
)
Spacer(modifier = Modifier.height(16.dp))
NormalLogin(onNavigateToNormalLogin, Type.NORMAL_VOLUNTEER)
NormalLogin(onNavigateToNormalLogin, UserType.NORMAL_VOLUNTEER)
}
}

@Composable
private fun Intermediator(
onNavigateToNormalLogin: (Type) -> Unit,
onNavigateToSignup: (Type) -> Unit
onNavigateToNormalLogin: (UserType) -> Unit,
onNavigateToSignup: (UserType) -> Unit
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
Expand All @@ -200,15 +200,15 @@ private fun Intermediator(
.fillMaxWidth()
.padding(horizontal = 20.dp),
content = stringResource(id = R.string.signup_with_connectdog),
onClick = { onNavigateToSignup(Type.INTERMEDIATOR) }
onClick = { onNavigateToSignup(UserType.INTERMEDIATOR) }
)
Spacer(modifier = Modifier.height(16.dp))
NormalLogin(onNavigateToNormalLogin, Type.INTERMEDIATOR)
NormalLogin(onNavigateToNormalLogin, UserType.INTERMEDIATOR)
}
}

@Composable
private fun NormalLogin(onNavigateToNormalLogin: (Type) -> Unit, type: Type) {
private fun NormalLogin(onNavigateToNormalLogin: (UserType) -> Unit, userType: UserType) {
ClickableText(
text = buildAnnotatedString {
withStyle(
Expand All @@ -229,6 +229,6 @@ private fun NormalLogin(onNavigateToNormalLogin: (Type) -> Unit, type: Type) {
append("로그인")
}
},
onClick = { onNavigateToNormalLogin(type) }
onClick = { onNavigateToNormalLogin(userType) }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import com.kusitms.connectdog.core.designsystem.component.ConnectDogNormalButton
import com.kusitms.connectdog.core.designsystem.component.ConnectDogTextField
import com.kusitms.connectdog.core.designsystem.component.ConnectDogTopAppBar
import com.kusitms.connectdog.core.designsystem.component.TopAppBarNavigationType
import com.kusitms.connectdog.core.util.Type
import com.kusitms.connectdog.core.util.UserType
import com.kusitms.connectdog.feature.login.viewmodel.LoginViewModel

private const val TAG = "EmailLoginScreen"

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun NormalLoginScreen(
type: Type,
userType: UserType,
onBackClick: () -> Unit,
onNavigateToVolunteerHome: () -> Unit,
onNavigateToIntermediatorHome: () -> Unit = {},
Expand All @@ -49,8 +49,8 @@ fun NormalLoginScreen(

isLoginSuccessful?.let {
if (it) {
when (type) {
Type.INTERMEDIATOR -> onNavigateToIntermediatorHome()
when (userType) {
UserType.INTERMEDIATOR -> onNavigateToIntermediatorHome()
else -> onNavigateToVolunteerHome()
}
}
Expand All @@ -65,25 +65,25 @@ fun NormalLoginScreen(
),
topBar = {
ConnectDogTopAppBar(
titleRes = when (type) {
Type.SOCIAL_VOLUNTEER -> R.string.volunteer_login
Type.NORMAL_VOLUNTEER -> R.string.volunteer_login
Type.INTERMEDIATOR -> R.string.intermediator_login
titleRes = when (userType) {
UserType.SOCIAL_VOLUNTEER -> R.string.volunteer_login
UserType.NORMAL_VOLUNTEER -> R.string.volunteer_login
UserType.INTERMEDIATOR -> R.string.intermediator_login
},
navigationType = TopAppBarNavigationType.BACK,
navigationIconContentDescription = "Navigation icon",
onNavigationClick = onBackClick
)
}
) {
Content(viewModel, type, isLoginSuccessful)
Content(viewModel, userType, isLoginSuccessful)
}
}

@Composable
private fun Content(
viewModel: LoginViewModel,
type: Type,
userType: UserType,
isLoginSuccessful: Boolean?
) {
Column(
Expand Down Expand Up @@ -118,10 +118,10 @@ private fun Content(
content = "로그인",
color = MaterialTheme.colorScheme.primary,
onClick = {
when (type) {
Type.INTERMEDIATOR -> { viewModel.initIntermediatorLogin() }
Type.NORMAL_VOLUNTEER -> { viewModel.initVolunteerLogin() }
Type.SOCIAL_VOLUNTEER -> {}
when (userType) {
UserType.INTERMEDIATOR -> { viewModel.initIntermediatorLogin() }
UserType.NORMAL_VOLUNTEER -> { viewModel.initVolunteerLogin() }
UserType.SOCIAL_VOLUNTEER -> {}
}
},
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.kusitms.connectdog.core.data.api.model.NormalLoginBody
import com.kusitms.connectdog.core.data.api.model.SocialLoginBody
import com.kusitms.connectdog.core.data.repository.DataStoreRepository
import com.kusitms.connectdog.core.data.repository.LoginRepository
import com.kusitms.connectdog.core.util.AppMode
import com.navercorp.nid.NaverIdLoginSDK
import com.navercorp.nid.oauth.NidOAuthLogin
import com.navercorp.nid.oauth.OAuthLoginCallback
Expand Down Expand Up @@ -65,6 +66,7 @@ constructor(
try {
val response = loginRepository.postLoginData(NormalLoginBody(_email.value, _password.value))
dataStoreRepository.saveAccessToken(response.accessToken)
dataStoreRepository.saveAppMode(AppMode.VOLUNTEER)
_isLoginSuccessful.value = true
Log.d(TAG, isLoginSuccessful.toString())
Log.d(TAG, dataStoreRepository.accessTokenFlow.first().toString())
Expand Down
1 change: 1 addition & 0 deletions feature/main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
implementation(project(":core:model"))
implementation(project(":core:designsystem"))
implementation(project(":core:util"))
implementation(project(":core:data"))

implementation(libs.androidx.core.splashscreen)
implementation("com.google.firebase:firebase-auth:22.3.0")
Expand Down
Loading

0 comments on commit 4cf46a5

Please sign in to comment.