Skip to content

Instantly share code, notes, and snippets.

View adrianhall's full-sized avatar

Adrian Hall adrianhall

View GitHub Profile
// Configure the Google Sign in requirements
val googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(resources.getString(R.string.google_client))
.requestEmail()
.build()
val googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions)
loginFormGoogleLoginButton.onClick {
startActivityForResult(googleSignInClient.signInIntent, GOOGLE_SIGN_IN)
}
// Register the callback for the Facebook authentication
LoginManager.getInstance().registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(result: LoginResult?) {
result?.let { model.federateWithFacebook(it.accessToken) }
[email protected]()
}
override fun onCancel() {
Log.d(TAG, "Facebook authentication is cancelled")
toast("Facebook Authentication is cancelled")
}
/**
* Federate with Facebook authentication
*/
override fun federateWithFacebook(accessToken: AccessToken) {
Log.d(TAG, "Federating with Facebook")
thread(start = true) {
with(service.identityManager.underlyingProvider) {
clear()
withLogins(mapOf("graph.facebook.com" to accessToken.token))
refresh()
// Register the callback for the Facebook authentication
LoginManager.getInstance().registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(result: LoginResult?) {
Log.d(TAG, "Facebook token = ${result?.accessToken?.token}")
toast("Access Token received")
}
override fun onCancel() {
Log.d(TAG, "Facebook authentication is cancelled")
toast("Facebook Authentication is cancelled")
// Facebook Requirement
private val callbackManager: CallbackManager = CallbackManager.Factory.create()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// Handle response from Facebook SDK
callbackManager.onActivityResult(requestCode, resultCode, data)
super.onActivityResult(requestCode, resultCode, data)
}
<!-- Facebook Requirements -->
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"/>
custom:
amplify:
- filename: ../android/app/src/main/res/raw/awsconfiguration.json
type: native
appClient: AndroidUserPoolClient
- filename: ../ios/App/awsconfiguration.json
type: native
appClient: iOSUserPoolClient
- filename: ../web/src/aws-exports.js
type: javascript
@SuppressLint("InflateParams")
private fun handleLogin() {
model.initiateSignin { identityRequest, params, callback ->
when(identityRequest) {
IdentityRequest.NEED_CREDENTIALS -> {
callback(mapOf(
"username" to loginFormUsernameField.text.toString(),
"password" to loginFormPasswordField.text.toString()
))
}
class AuthenticatorViewModel(private val identityRepository: IdentityRepository) : ViewModel() {
/**
* Current user record, or null if the user is not logged in
*/
val currentUser: LiveData<User?> = identityRepository.currentUser
/**
* Stored user name, or null if the user has never logged in
*/
val storedUsername: LiveData<String?> = identityRepository.storedUsername
override fun initiateSignin(handler: IdentityHandler) {
try {
userPool.currentUser.getSessionInBackground(object : AuthenticationHandler {
override fun onSuccess(nSession: CognitoUserSession?, newDevice: CognitoDevice?) {
val userSession = checkNotNull(nSession) { "user session is null" }
storeUserSession(handler, userSession)
runOnUiThread { handler(IdentityRequest.SUCCESS, null, DO_NOTHING) }
}
override fun onFailure(exception: Exception?) = handleFailure(handler, exception?.message)