Skip to content

Commit

Permalink
Merge pull request #43 from snuhcs-course/refactor/auth-clean-ver
Browse files Browse the repository at this point in the history
유저 인증 관련 클래스 기능 구현 & 리팩토링
  • Loading branch information
yjeong-k authored Nov 16, 2023
2 parents 030281e + 53ee7d1 commit 6b455a9
Show file tree
Hide file tree
Showing 80 changed files with 2,238 additions and 1,108 deletions.
3 changes: 1 addition & 2 deletions backend/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ class Meta:
fields = (
'id',
'email',
'nickname',
'updated_at',
'nickname'
)


Expand Down
4 changes: 2 additions & 2 deletions backend/user/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_get_profile_success(self):
}
response = self.client.get('/user/profile/', **headers)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()['user']
data = response.json()
self.assertEqual(self.user.email, data['email'])
self.assertEqual(self.user.nickname, data['nickname'])

Expand Down Expand Up @@ -93,7 +93,7 @@ def test_change_nickname_success(self):

# Call get-profile API to check whether the value change is applied
response = self.client.get('/user/profile/', **headers)
self.assertEqual(data['nickname'], response.json()['user']['nickname'])
self.assertEqual(data['nickname'], response.json()['nickname'])

def test_change_password_success(self):
headers = {
Expand Down
3 changes: 1 addition & 2 deletions backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ class UserProfileView(APIView):
def get(self, request):
user = request.user
serialized_user = UserProfileSerializer(user).data
response_data = {"user": serialized_user}
return Response(response_data, status=status.HTTP_200_OK)
return Response(serialized_user, status=status.HTTP_200_OK)


class PasswordUpdateView(APIView):
Expand Down
7 changes: 6 additions & 1 deletion frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ dependencies {
// Gson
implementation("com.google.code.gson:gson:2.9.0")

// DataStore
val dataStoreVersion = "1.0.0-alpha06"
implementation("androidx.datastore:datastore-preferences:$dataStoreVersion")
implementation("androidx.datastore:datastore-preferences-core:$dataStoreVersion")

// Room
val roomVersion = "2.6.0"
implementation("androidx.room:room-ktx:$roomVersion")
Expand All @@ -130,7 +135,7 @@ dependencies {
implementation("com.google.dagger:hilt-android:$hiltVersion")
kapt("com.google.dagger:hilt-compiler:$hiltVersion")

implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation("androidx.hilt:hilt-navigation-compose:1.1.0")

// MockK
val mockkVersion = "1.13.8"
Expand Down
5 changes: 3 additions & 2 deletions frontend/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:name=".BaseApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -16,8 +16,9 @@
android:theme="@style/Theme.SpeechBuddy"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity android:name=".HomeActivity" />
<activity
android:name=".MainActivity"
android:name=".AuthActivity"
android:exported="true"
android:theme="@style/Theme.SpeechBuddy">
<intent-filter>
Expand Down
50 changes: 50 additions & 0 deletions frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.speechbuddy

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.core.view.WindowCompat
import com.example.speechbuddy.compose.SpeechBuddyAuth
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AuthActivity : BaseActivity() {

private val loginViewModel: LoginViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Displaying edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)

subscribeObservers()
checkPreviousAuthUser()

setContent {
SpeechBuddyTheme {
SpeechBuddyAuth()
}
}
}

private fun subscribeObservers() {
sessionManager.isAuthorized.observe(this) { isAuthorized ->
if (isAuthorized) navHomeActivity()
}
}

private fun navHomeActivity() {
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}

private fun checkPreviousAuthUser() {
loginViewModel.checkPreviousUser()
}

}
12 changes: 12 additions & 0 deletions frontend/app/src/main/java/com/example/speechbuddy/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.speechbuddy

import androidx.appcompat.app.AppCompatActivity
import com.example.speechbuddy.domain.SessionManager
import javax.inject.Inject

abstract class BaseActivity : AppCompatActivity() {

@Inject
lateinit var sessionManager: SessionManager

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.speechbuddy

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class BaseApplication : Application()
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
package com.example.speechbuddy

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.view.WindowCompat
import com.example.speechbuddy.MainApplication.Companion.token_prefs
import com.example.speechbuddy.compose.SpeechBuddyApp
import com.example.speechbuddy.domain.utils.TokenSharedPreferences
import com.example.speechbuddy.compose.SpeechBuddyHome
import com.example.speechbuddy.ui.SpeechBuddyTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
class HomeActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
token_prefs = TokenSharedPreferences(applicationContext)
super.onCreate(savedInstanceState)

// Displaying edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
SpeechBuddyTheme {
SpeechBuddyApp()
SpeechBuddyHome()
}
}

subscribeObservers()
}

private fun subscribeObservers() {
sessionManager.isAuthorized.observe(this) { isAuthorized ->
if (!isAuthorized) navAuthActivity()
}
}

private fun navAuthActivity() {
val intent = Intent(this, AuthActivity::class.java)
startActivity(intent)
finish()
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,33 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.speechbuddy.compose.emailverification.EmailVerificationScreen
import com.example.speechbuddy.compose.home.HomeScreen
import com.example.speechbuddy.compose.landing.LandingScreen
import com.example.speechbuddy.compose.login.LoginScreen
import com.example.speechbuddy.compose.resetpassword.ResetPasswordScreen
import com.example.speechbuddy.compose.signup.SignupScreen

@Composable
fun SpeechBuddyApp() {
fun SpeechBuddyAuth() {
val navController = rememberNavController()
SpeechBuddyNavHost(
SpeechBuddyAuthNavHost(
navController = navController
)
}

@Composable
fun SpeechBuddyNavHost(
fun SpeechBuddyAuthNavHost(
navController: NavHostController
) {
// val activity = (LocalContext.current as Activity)
NavHost(navController = navController, startDestination = "landing") {
composable("landing") {
LandingScreen(
onGuestClick = {
navController.navigate("home")
},
onLoginClick = {
navController.navigate("login")
}
)
}
composable("login") {
LoginScreen(
onBackClick = {
navController.navigateUp()
},
onResetPasswordClick = {
navController.navigate("email_verification/reset_password")
},
Expand All @@ -53,31 +45,24 @@ fun SpeechBuddyNavHost(
val source = backStackEntry.arguments?.getString("source")
EmailVerificationScreen(
source = source,
onBackClick = {
navController.navigateUp()
},
navController = navController,
navigateCallback = { navController.navigate(it) }
)
}
composable("signup/{emailInput}") { backStackEntry ->
val emailInput = backStackEntry.arguments?.getString("emailInput")
composable("signup/{email}") { backStackEntry ->
val email = backStackEntry.arguments?.getString("email")
SignupScreen(
onBackClick = {
navController.navigateUp()
},
email = emailInput ?: ""
email = email ?: "",
navigateToLogin = {
navController.navigate("login")
}
)
}
composable("reset_password") {
ResetPasswordScreen(
onBackClick = {
navController.navigateUp()
},
navController = navController
navigateToLogin = {
navController.navigate("login")
}
)
}
composable("home") {
HomeScreen()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.speechbuddy.compose.home
package com.example.speechbuddy.compose

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -36,7 +38,7 @@ data class BottomNavItem(

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen() {
fun SpeechBuddyHome() {
val navController = rememberNavController()
val navItems = listOf(
BottomNavItem(
Expand All @@ -51,7 +53,7 @@ fun HomeScreen() {
),
BottomNavItem(
"symbol_creation",
R.string.symbol_creation,
R.string.create_new_symbol,
R.drawable.outline_add_a_photo_24
),
BottomNavItem(
Expand All @@ -62,6 +64,7 @@ fun HomeScreen() {
)

Scaffold(
modifier = Modifier.padding(bottom = 48.dp), // System Bar Padding
bottomBar = {
BottomNavigationBar(
items = navItems,
Expand All @@ -72,7 +75,7 @@ fun HomeScreen() {
)
}
) { paddingValues ->
HomeScreenNavHost(
SpeechBuddyHomeNavHost(
navController = navController,
bottomPaddingValues = paddingValues
)
Expand All @@ -88,8 +91,9 @@ private fun BottomNavigationBar(
val backStackEntry = navController.currentBackStackEntryAsState()

NavigationBar(
modifier = Modifier.height(64.dp),
containerColor = MaterialTheme.colorScheme.secondaryContainer,
windowInsets = WindowInsets(top = 20.dp)
windowInsets = WindowInsets(top = 16.dp)
) {
items.forEach { item ->
val selected = item.route == backStackEntry.value?.destination?.route
Expand All @@ -102,10 +106,10 @@ private fun BottomNavigationBar(
contentDescription = stringResource(id = item.nameResId)
)
},
modifier = Modifier.size(40.dp),
modifier = Modifier.size(32.dp),
colors = NavigationBarItemDefaults.colors(
selectedIconColor = MaterialTheme.colorScheme.onPrimaryContainer,
unselectedIconColor = MaterialTheme.colorScheme.outline
selectedIconColor = MaterialTheme.colorScheme.onSecondaryContainer,
unselectedIconColor = MaterialTheme.colorScheme.onSecondaryContainer.copy(alpha = .5f)
),
interactionSource = NoRippleInteractionSource()
)
Expand All @@ -114,7 +118,7 @@ private fun BottomNavigationBar(
}

@Composable
private fun HomeScreenNavHost(
private fun SpeechBuddyHomeNavHost(
navController: NavHostController,
bottomPaddingValues: PaddingValues
) {
Expand Down
Loading

0 comments on commit 6b455a9

Please sign in to comment.