Skip to content

Commit

Permalink
Merge pull request #61 from snuhcs-course/frontend/refactor-login
Browse files Browse the repository at this point in the history
Login / Register 정리
  • Loading branch information
heka1024 authored Dec 6, 2023
2 parents 8a4c128 + 4024cf0 commit 9a4ef18
Show file tree
Hide file tree
Showing 21 changed files with 357 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.frontend

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.frontend.repository.UserContextRepository
import com.example.frontend.ui.login.LoginActivity

@SuppressLint("CustomSplashScreen")
Expand All @@ -13,11 +13,9 @@ class SplashScreenActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Check if the user is already logged in
val sharedPreferences = getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
val isLoggedIn = sharedPreferences.getBoolean("IS_LOGGED_IN", false)
val userContextRepository = UserContextRepository.ofContext(this)

if (isLoggedIn) {
if (userContextRepository.getIsLoggedIn()) {
// User is already logged in, skip the login activity
startActivity(Intent(this, MapActivity::class.java))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface AuthService {
fun rename(@Body newName: String?)

@POST("/verification_mails")
fun verifyEmail(@Body email: String): Call<EmailModel?>?
fun verifyEmail(@Body email: EmailModel): Call<EmailModel?>

@POST("/users")
fun register(@Body registerModel: RegisterModel?): Call<RegisterModel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.example.frontend.model

import com.google.android.gms.maps.model.LatLng

data class AuthResponse(
val token: String,
val userName: String,
val userCurrentLocation: LatLng?,
val userMail: String,
val userProfile: Int
)
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.frontend.repository

import android.content.Context
import com.example.frontend.utilities.EncryptedSharedPreferenceKVStore
import com.example.frontend.utilities.KVStore
import com.example.frontend.utilities.SharedPreferenceKVStore

Expand All @@ -24,6 +25,10 @@ class UserContextRepository(
return store.getString(AUTH_TOKEN) ?: ""
}

fun saveAuthToken(token: String) {
store.putString(AUTH_TOKEN, token)
}

fun saveSelectedPredefinedImage(imageId: Int?) {
if (imageId != null && imageId != KVStore.DEFAULT_INT) {
store.putInt(SELECTED_PREDEFINED_IMAGE, imageId)
Expand All @@ -35,13 +40,28 @@ class UserContextRepository(
return if (imageId != KVStore.DEFAULT_INT) imageId else null
}

fun saveUserMail(userMail: String) {
store.putString(USER_MAIL, userMail)
}

fun saveIsLoggedIn(isLoggedIn: Boolean) {
store.putBoolean(IS_LOGGED_IN, isLoggedIn)
}

fun getIsLoggedIn(): Boolean {
return store.getBoolean(IS_LOGGED_IN) ?: false
}

companion object {
/*
* Returns UserContextRepository instance backed by SharedPreferenceKVStore.
*/
fun ofContext(context: Context): UserContextRepository {
val sharedPreferencesKVStore = SharedPreferenceKVStore(context)
return UserContextRepository(sharedPreferencesKVStore)
fun ofContext(context: Context, secure: Boolean = false): UserContextRepository {
val store = if (secure)
EncryptedSharedPreferenceKVStore(context)
else
SharedPreferenceKVStore(context)
return UserContextRepository(store)
}

private const val USERNAME = "USERNAME"
Expand All @@ -50,5 +70,6 @@ class UserContextRepository(
const val DEFAULT_USER_MAIL = "[email protected]"
private const val AUTH_TOKEN = "AUTH_TOKEN"
private const val SELECTED_PREDEFINED_IMAGE = "SELECTED_PREDEFINED_IMAGE"
private const val IS_LOGGED_IN = "IS_LOGGED_IN"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fun LoginScreen() {

when (currentComponent.value) {
LoginComponent.LOGIN -> LoginUI()
LoginComponent.REGISTER -> RegisterUI(onRegisterClick)
LoginComponent.REGISTER -> RegisterUI(onLoginClick)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Lock
Expand All @@ -21,20 +23,21 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.frontend.ui.component.CustomButton
import com.example.frontend.ui.theme.FrontendTheme
import com.example.frontend.usecase.LoginUseCase
import com.example.frontend.usecase.login.LoginUseCase

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoginUI() {
var context = LocalContext.current
val context = LocalContext.current
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var response = remember { mutableStateOf("") }
val response = remember { mutableStateOf("") }

Column(
horizontalAlignment = Alignment.CenterHorizontally
Expand All @@ -43,21 +46,30 @@ fun LoginUI() {
value = email,
onValueChange = { email = it },
label = { Text("Email Address") },
leadingIcon = {
Icon(imageVector = Icons.Default.Email, contentDescription = null)
},
leadingIcon = { Icon(imageVector = Icons.Default.Email, contentDescription = null) },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
)

Spacer(modifier = Modifier.height(20.dp))

OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
leadingIcon = {
Icon(imageVector = Icons.Default.Lock, contentDescription = null)
},
visualTransformation = PasswordVisualTransformation()
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = { LoginUseCase(context, email, password, response).execute() }
)
)

Spacer(modifier = Modifier.height(140.dp))

CustomButton(
buttonText = "Login",
onClickHandler = { LoginUseCase(context, email, password, response).execute() }
Expand Down
Loading

0 comments on commit 9a4ef18

Please sign in to comment.