-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature: #8 로그인 화면 구현
- Loading branch information
Showing
12 changed files
with
224 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,3 +88,4 @@ lint/tmp/ | |
|
||
# Mac | ||
*.DS_Store | ||
/presentation/src/main/java/com/gta/presentation/secret/Secrets.kt |
22 changes: 0 additions & 22 deletions
22
data/src/androidTest/java/com/gta/data/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
presentation/src/main/java/com/gta/presentation/di/FirebaseAuthModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.gta.presentation.di | ||
|
||
import com.google.firebase.auth.FirebaseAuth | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import java.util.Locale | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object FirebaseAuthModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFirebaseAuth() = FirebaseAuth.getInstance().apply { | ||
setLanguageCode(Locale.getDefault().language) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
presentation/src/main/java/com/gta/presentation/di/FirebaseSigninModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.gta.presentation.di | ||
|
||
import android.content.Context | ||
import com.google.android.gms.auth.api.signin.GoogleSignIn | ||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions | ||
import com.gta.presentation.secret.FIREBASE_CLIENT_ID | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ActivityComponent | ||
import dagger.hilt.android.qualifiers.ActivityContext | ||
|
||
@Module | ||
@InstallIn(ActivityComponent::class) | ||
class FirebaseSigninModule { | ||
@Provides | ||
fun provideGoogleSignInOptions() = GoogleSignInOptions | ||
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestIdToken(FIREBASE_CLIENT_ID) | ||
.requestEmail() | ||
.build() | ||
|
||
@Provides | ||
fun provideGoogleSignInClient(@ActivityContext context: Context, options: GoogleSignInOptions) = | ||
GoogleSignIn.getClient(context, options) | ||
} |
20 changes: 0 additions & 20 deletions
20
presentation/src/main/java/com/gta/presentation/ui/LoginActivity.kt
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
presentation/src/main/java/com/gta/presentation/ui/login/LoginActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.gta.presentation.ui.login | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.activity.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import com.google.android.gms.auth.api.Auth | ||
import com.google.android.gms.auth.api.signin.GoogleSignInClient | ||
import com.gta.presentation.databinding.ActivityLoginBinding | ||
import com.gta.presentation.ui.MainActivity | ||
import com.gta.presentation.ui.base.BaseActivity | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate) { | ||
|
||
@Inject | ||
lateinit var googleSignInClient: GoogleSignInClient | ||
|
||
private val viewModel: LoginViewModel by viewModels() | ||
|
||
private val requestActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
if (it.resultCode == RESULT_OK) { | ||
val authIntent = it.data ?: return@registerForActivityResult | ||
val account = Auth.GoogleSignInApi.getSignInResultFromIntent(authIntent)?.signInAccount | ||
viewModel.signinWithToken(account?.idToken) | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initCollector() | ||
binding.btnLoginGoogle.setOnClickListener { | ||
googleLogin() | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
viewModel.checkLoginState() | ||
} | ||
|
||
private fun initCollector() { | ||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.loginEvent.collectLatest { state -> | ||
/* | ||
로그인에 성공 했다면 | ||
1. real time db에 회원정보가 저장되어있는지 확인 | ||
2. 있으면 바로 화면 전환 | ||
3. 없으면 db에 회원정보 생성 후 화면 전환 | ||
*/ | ||
if (state) { | ||
startMainActivity() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun startMainActivity() { | ||
val intent = Intent(this, MainActivity::class.java) | ||
startActivity(intent) | ||
finish() | ||
} | ||
|
||
private fun googleLogin() { | ||
requestActivity.launch(googleSignInClient.signInIntent) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
presentation/src/main/java/com/gta/presentation/ui/login/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.gta.presentation.ui.login | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.GoogleAuthProvider | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class LoginViewModel @Inject constructor( | ||
private val auth: FirebaseAuth | ||
) : ViewModel() { | ||
|
||
private val _loginEvent = MutableSharedFlow<Boolean>() | ||
val loginEvent: SharedFlow<Boolean> get() = _loginEvent | ||
|
||
fun checkLoginState() { | ||
emitLoginEvent(auth.currentUser != null) | ||
} | ||
|
||
fun signinWithToken(token: String?) { | ||
token ?: return | ||
val credential = GoogleAuthProvider.getCredential(token, null) | ||
auth.signInWithCredential(credential).addOnCompleteListener { task -> | ||
if (task.isSuccessful) { | ||
emitLoginEvent(true) | ||
} else { | ||
Timber.e(task.exception) | ||
} | ||
} | ||
} | ||
|
||
private fun emitLoginEvent(state: Boolean) { | ||
viewModelScope.launch { | ||
_loginEvent.emit(state) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="160dp" | ||
android:height="112dp" | ||
android:viewportWidth="160" | ||
android:viewportHeight="112"> | ||
<path | ||
android:pathData="M136,40H132L104.81,6.01C103.31,4.13 101.4,2.62 99.24,1.58C97.08,0.54 94.71,0 92.31,0H38.83C32.29,0 26.41,3.98 23.98,10.06L12,40.56C5.11,42.35 0,48.55 0,56V84C0,86.21 1.79,88 4,88H16C16,101.25 26.75,112 40,112C53.26,112 64,101.25 64,88H96C96,101.25 106.75,112 120,112C133.26,112 144,101.25 144,88H156C158.21,88 160,86.21 160,84V64C160,50.74 149.26,40 136,40ZM40,100C33.38,100 28,94.62 28,88C28,81.38 33.38,76 40,76C46.62,76 52,81.38 52,88C52,94.62 46.62,100 40,100ZM58,40H29.23L38.83,16H58V40ZM70,40V16H92.31L111.51,40H70ZM120,100C113.38,100 108,94.62 108,88C108,81.38 113.38,76 120,76C126.62,76 132,81.38 132,88C132,94.62 126.62,100 120,100Z" | ||
android:fillColor="@color/primaryColor"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters