ccoenraets / OpenFB

Micro-library that lets you integrate browser and Cordova apps with Facebook with no plugin or SDK dependency.
MIT License
507 stars 229 forks source link

How to make openFB use native app for login instead of in-app-browser (ionic) #126

Open pedromonteiro221 opened 7 years ago

pedromonteiro221 commented 7 years ago

Hello everyone, i am having some trouble with the ngopenFB (openFB)... I followed this tutorial: https://ccoenraets.github.io/ionic-tutorial/ionic-facebook-integration.html

I wanted to make a login that when the user presses a button to login, show up a window of the native app with a button to login instead of a window in-app-browser to make user input his data to login.

What i want is this: https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/12/1449042836fb-login.png

But what i have is this: https://www.tutorialspoint.com/ionic/images/ionic-cordova-facebook.jpg

Login and logout are working perfectly fine, and im able to get onto my app user info, but i wanted to have a login that doens't need user input.

Im sorry if i didn't explain myself, I'm very new to ionic and these plugins...

blueprin4 commented 7 years ago

i need same

pedromonteiro221 commented 7 years ago

Sorry but this plugin doens't have native login, if you want to use a good plugin that uses native login / web login use this: https://github.com/jeduan/cordova-plugin-facebook4

Toomavic commented 7 years ago

Could you provide a guide how to use it with ionic project? I had read docs but still i dont know how to start

pedromonteiro221 commented 7 years ago

This is for ionic v1

First you need to create your app on developers facebook and get an AppID (you must see a guide how to configure developers facebook, i started with web app then configured it for android)

After that on your ionic project you do cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication" where AppID and AppName are provided when you create your app on developers facebook.

Then you basically after having all configured, code the login, getLoginStatus, logout:

$scope.fbLogin = function () {
        facebookConnectPlugin.login(['email'], function (response) {
            console.log(response);
            $window.localStorage['status'] = "fbtrue";
            $rootScope.isLogin = true;
            $state.go($state.current, {}, { reload: true });
            $scope.getFbData();
            useremail = $window.localStorage['email'];
            username = $window.localStorage['name'];
            userimage = $window.localStorage['userimage'];
            token = $window.localStorage['AccessToken'];
            console.log(token);
            $window.localStorage['log'] = "true";
        }, function (error) {
        })
    };

    $scope.getFbData = function () {
        facebookConnectPlugin.getLoginStatus(function (response) {
            if (response.status == "connected") {
                $window.localStorage['AccessToken'] = response.authResponse.accessToken;
                facebookConnectPlugin.api('/' + response.authResponse.userID + '?fields=id,name,email', [],
                function onSuccess(result) {
                    $window.localStorage['name'] = result.name;
                    $window.localStorage['email'] = result.email;
                    $window.localStorage['userimage'] = "http://graph.facebook.com/" + response.authResponse.userID + "/picture?width=80&height=80";

                    $window.localStorage['log'] = "true";
                    $rootScope.email = $window.localStorage['email'];
                    $scope.username = $window.localStorage['name'];
                    $scope.useremail = $window.localStorage['email'];
                    $scope.userimage = $window.localStorage['userimage'];

                    $state.go($state.current, {}, { reload: true });
                },
                function onError(error) {
                }
                );
            } else {
            }
        }, function (error) { })
    }

$scope.Logout = function () {
        if ($window.localStorage['status'] == "fbtrue") {
            facebookConnectPlugin.logout(function (success) {
                $rootScope.isLogin = false;
                $window.localStorage['status'] = "false";
                $window.localStorage['log'] = "false";
                $window.localStorage['AccessToken'] = "";
                $state.go($state.current, {}, { reload: true });
            }, function (error) { })
        }
  }