Wizcorp / phonegap-facebook-plugin

The official plugin for Facebook in Apache Cordova/PhoneGap
Other
1.91k stars 2.01k forks source link

Persistent Login #1244

Closed ivanssa closed 8 years ago

ivanssa commented 8 years ago

Hello congratulations for the work, I use the log in my successful app, I always have to ask the user login at the beginning, so I wonder if it is possible to record the token localstorage on first login and always use the file to enter the app automatically until the user logs cleaning localstorage the logout and oh yes it again request the token through facebook, is this possible?

ivanssa commented 8 years ago

hello after research days solved the problem using the automatic log recording a token and using it in the next automatic logins: ref.authWithCustomToken until the user can log out and clear the variable token recorded in localStorage, is a tip for those who need in the future ...

kossanah commented 8 years ago

Please share your code, I like to see a sample how you did the authentication call after storing the token

ivanssa commented 8 years ago

Hello @kossanah will help you because I fought a lot to achieve reach this work code and it may be that several people may need and help to improve, so actually the facebook plugin only provides access through the network, to meet the automatic login I did through token used a firebase function to generate the token and so can automatically log my user logins in the next following code:

I initially created a service on my app.js to generate the access token.

MyApp.factory('AcessToken', function($localstorage){ 

  var ref = new Firebase('https://MyApp.firebaseio.com');

  return {
    setToken: function(myid){       
    var tokenGenerator = new FirebaseTokenGenerator("MyToken_Secret");
    var token = tokenGenerator.createToken({uid: myid});                    
    $localstorage.set('token',token);
    }
  }
})

in my controler Login I added AcessToken service the dependencies and made the token charge recorded in localstorage then I access a function that attempts to automatic login if the token was recorded in localStorage if so he logs in using the token using the function of firebase otherwise token it calls the normal login using facebok plugin that records the token that will be using in the next automatic login so every time the User enter the next time, also added logout only clean the variable thus returning token the normal login sistem using the plugin in the next access check.

.controller('LoginCtrl', function($scope, $rootScope,$state, $stateParams,$ionicLoading,$filter,$firebaseAuth,$cordovaFacebook,$ionicPopup,$timeout,$localstorage,AcessToken) {

$scope.token = $localstorage.get('token');
var ref = new Firebase("https://myapp.firebaseio.com/");

// IF LOGIN PERSISTENT  (TOKEN)

if(($scope.token!=undefined)&&($scope.token!='undefined')){     
    $ionicLoading.show();               
    setTimeout(function(){
        ref.authWithCustomToken($scope.token, function(error, authData) {
          if (error) {            
              console.log(error.code);
              $ionicLoading.hide(); 

            } else {
                $ionicLoading.hide();                   
                $state.go('app.home');  
    }
    });
    $ionicLoading.hide();
    },100);                     
    }

// IF NORMAL LOGIN (PLUGIN) 

 facebookConnectPlugin.login(['public_profile'], function(status) {
      facebookConnectPlugin.getAccessToken(function(token) {
            ref.authWithOAuthToken("facebook", token, function(error, authData) {
              if (error) {
                alert('error');
              } else {
                                alert('sucess');
                AcessToken.setToken(authData.uid);
              }

....

// DESACTIVE LOGIN PERSISTENT 

$scope.logout = function(){
$localstorage.set('token',undefined);
};

Let me know if you get hit, or may well help to improve the code, hugs and good work.