Skip to content

Commit

Permalink
[FEATURE] #87 : 현재 로컬에 저장된 정보를 바탕으로, 현재 로그인된 회원인지 검증 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Jan 11, 2024
1 parent 0820bd7 commit ed90897
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.wap.wapp.core.data.repository.auth

interface AuthRepository {
suspend fun hasPendingResult(): Boolean

suspend fun signIn(email: String): Result<String>

suspend fun signOut(): Result<Unit>

suspend fun deleteUser(): Result<Unit>

suspend fun isUserSignIn(): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import javax.inject.Inject
class AuthRepositoryImpl @Inject constructor(
private val authDataSource: AuthDataSource,
) : AuthRepository {
override suspend fun hasPendingResult(): Boolean = authDataSource.hasPendingResult()

override suspend fun signIn(email: String): Result<String> = authDataSource.signIn(email)

override suspend fun signOut(): Result<Unit> = authDataSource.signOut()

override suspend fun deleteUser(): Result<Unit> = authDataSource.deleteUser()

override suspend fun isUserSignIn(): Result<Boolean> = authDataSource.isUserSignIn()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.wap.wapp.core.network.source.auth

interface AuthDataSource {
suspend fun hasPendingResult(): Boolean

suspend fun signIn(email: String): Result<String>

suspend fun signOut(): Result<Unit>

suspend fun deleteUser(): Result<Unit>

suspend fun isUserSignIn(): Result<Boolean>
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
package com.wap.wapp.core.network.source.auth

import android.app.Activity
import android.content.Context
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.OAuthProvider
import com.google.firebase.auth.FirebaseAuth.AuthStateListener
import com.wap.wapp.core.network.utils.await
import dagger.hilt.android.qualifiers.ActivityContext
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import javax.inject.Inject

class AuthDataSourceImpl @Inject constructor(
private val firebaseAuth: FirebaseAuth,
@ActivityContext private val context: Context,
) : AuthDataSource {
override suspend fun hasPendingResult(): Boolean {
return firebaseAuth.pendingAuthResult != null
}

override suspend fun signIn(email: String): Result<String> {
return runCatching {
val provider = OAuthProvider.newBuilder("github.com")
provider.addCustomParameter("login", email)

val activityContext = context as Activity

val result = firebaseAuth.startActivityForSignInWithProvider(
activityContext,
provider.build(),
).await()

val user = checkNotNull(result.user)
user.uid
}
}

override suspend fun signOut(): Result<Unit> {
return runCatching {
firebaseAuth.signOut()
Expand All @@ -47,4 +24,29 @@ class AuthDataSourceImpl @Inject constructor(
.await()
}
}

@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun isUserSignIn(): Result<Boolean> = runCatching {
suspendCancellableCoroutine { cont ->
val authStateListener = object : AuthStateListener {
override fun onAuthStateChanged(remoteAuth: FirebaseAuth) {
val user = remoteAuth.currentUser
if (user != null) {
cont.resume(true, null)
} else {
cont.resume(false, null)
}

// 한 번 호출된 후에 Listener 삭제
firebaseAuth.removeAuthStateListener(this)
}
}

firebaseAuth.addAuthStateListener(authStateListener)

cont.invokeOnCancellation { // Coroutine이 취소되는 경우 리스너 삭제
firebaseAuth.removeAuthStateListener(authStateListener)
}
}
}
}

0 comments on commit ed90897

Please sign in to comment.