-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fa6bd8
commit 92aac34
Showing
18 changed files
with
188 additions
and
35 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
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 |
---|---|---|
|
@@ -10,5 +10,5 @@ interface AuthManager { | |
} | ||
|
||
enum class SocialProvider { | ||
GOOGLE, FACEBOOK | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/kotlin/net/rafaeltoledo/social/data/auth/FacebookAuth.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,61 @@ | ||
package net.rafaeltoledo.social.data.auth | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.util.Log | ||
import com.facebook.CallbackManager | ||
import com.facebook.FacebookCallback | ||
import com.facebook.FacebookException | ||
import com.facebook.login.LoginManager | ||
import com.facebook.login.LoginResult | ||
import java.util.concurrent.CountDownLatch | ||
|
||
/** | ||
* Authenticates a user using the Facebook Login SDK. | ||
*/ | ||
class FacebookAuth : DelegatedAuth { | ||
|
||
private lateinit var callbackManager: CallbackManager | ||
private lateinit var status: AuthResult | ||
private lateinit var countDownLatch: CountDownLatch | ||
|
||
private val callback = object : FacebookCallback<LoginResult> { | ||
override fun onSuccess(result: LoginResult?) { | ||
status = AuthResult(Status.SUCCESS, result?.accessToken?.token) | ||
countDownLatch.countDown() | ||
} | ||
|
||
override fun onCancel() { | ||
status = AuthResult(Status.CANCELED) | ||
countDownLatch.countDown() | ||
} | ||
|
||
override fun onError(error: FacebookException?) { | ||
Log.e("FacebookAuth", "Could not complete Facebook signin", error) | ||
status = AuthResult(Status.FAILURE) | ||
countDownLatch.countDown() | ||
} | ||
} | ||
|
||
override fun <T : DelegatedAuth> build(activity: Activity): T { | ||
callbackManager = CallbackManager.Factory.create() | ||
LoginManager.getInstance().registerCallback(callbackManager, callback) | ||
@Suppress("UNCHECKED_CAST") return this as T | ||
} | ||
|
||
override fun signIn(activity: Activity) { | ||
countDownLatch = CountDownLatch(1) | ||
LoginManager.getInstance().logInWithReadPermissions(activity, listOf("email", "public_profile")) | ||
} | ||
|
||
override fun onResult(requestCode: Int, resultCode: Int, data: Intent?): AuthResult { | ||
callbackManager.onActivityResult(requestCode, resultCode, data) | ||
countDownLatch.await() | ||
return status | ||
} | ||
|
||
override fun signOut(callback: (Status) -> Unit) { | ||
LoginManager.getInstance().logOut() | ||
callback(Status.SUCCESS) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package net.rafaeltoledo.social.di | ||
|
||
import net.rafaeltoledo.social.data.auth.DelegatedAuth | ||
import net.rafaeltoledo.social.data.auth.FacebookAuth | ||
import net.rafaeltoledo.social.data.auth.GoogleAuth | ||
import net.rafaeltoledo.social.data.auth.SocialProvider | ||
import org.koin.core.qualifier.named | ||
import org.koin.dsl.module | ||
|
||
val authModule = module { | ||
single<DelegatedAuth> { GoogleAuth() } | ||
single<DelegatedAuth>(named(SocialProvider.GOOGLE.name)) { GoogleAuth() } | ||
single<DelegatedAuth>(named(SocialProvider.FACEBOOK.name)) { FacebookAuth() } | ||
} |
5 changes: 4 additions & 1 deletion
5
app/src/main/kotlin/net/rafaeltoledo/social/di/ViewModelModule.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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package net.rafaeltoledo.social.di | ||
|
||
import net.rafaeltoledo.social.data.auth.SocialProvider | ||
import net.rafaeltoledo.social.ui.feature.main.MainViewModel | ||
import net.rafaeltoledo.social.ui.feature.signin.SignInViewModel | ||
import org.koin.androidx.viewmodel.dsl.viewModel | ||
import org.koin.core.qualifier.named | ||
import org.koin.dsl.module | ||
|
||
val viewModelModule = module { | ||
viewModel { MainViewModel(get()) } | ||
viewModel { SignInViewModel(get(), get()) } | ||
viewModel { SignInViewModel(get(), get(named(SocialProvider.GOOGLE.name)), get(named( | ||
SocialProvider.FACEBOOK.name))) } | ||
} |
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
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
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
Oops, something went wrong.