Search code examples
androidamazon-web-servicesfacebook-loginamazon-cognitoamazon-cognito-facebook

aws cognito with facebook login? - android


so im really confused on how the facebook login works with aws cognito in android. I was able to hook aws cognito up and the facebook log in just fine. The aws cognito guide gives me these lines of code:

 Map<String, String> logins = new HashMap<String, String>();
        logins.put("graph.facebook.com", AccessToken.getCurrentAccessToken().getToken());
        credentialsProvider.setLogins(logins);

couple questions: 1.Where do i put these lines of code? 2.How do i set up cognito user equal to the login facebook user? 3.And basically, whats a working example of this?

Hope you guys can help!


Solution

  • This is the following code where I have used the facebook login with federated identities from the congnito. first you need to set up the CognitoSyncManagerFile with the appropriate login credentials with the pool details . And then the following code as follows .

    //initialize the facebook SDK 
     FacebookSdk.sdkInitialize(getApplicationContext());
    
     //If access token is already here, set fb session
            final AccessToken fbAccessToken = AccessToken.getCurrentAccessToken();
            if (fbAccessToken != null) {
                setFacebookSession(fbAccessToken);
            //    btnLoginFacebook.setVisibility(View.GONE);
            }
    
    btnLoginFacebook = (Button) findViewById(R.id.btnLoginFacebook);
            btnLoginFacebook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // start Facebook Login
                    LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile"));
                    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            btnLoginFacebook.setVisibility(View.GONE);
                            new GetFbName(loginResult).execute();
                            setFacebookSession(loginResult.getAccessToken());
                        }
    
                        @Override
                        public void onCancel() {
                            Toast.makeText(MainActivity.this, "Facebook login cancelled",
                                    Toast.LENGTH_LONG).show();
                        }
    
                        @Override
                        public void onError(FacebookException error) {
                            Toast.makeText(MainActivity.this, "Error in Facebook login " +
                                    error.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
    
    
    
    
    
    //Set the session with the following method
          private void setFacebookSession(AccessToken accessToken) {
                    Log.i(TAG, "facebook token: " + accessToken.getToken());
                    CognitoSyncClientManager.addLogins("graph.facebook.com",
                            accessToken.getToken());
    
    
                }
    

    For more information follow the below url

    https://github.com/awslabs/aws-sdk-android-samples/tree/master/CognitoSyncDemo