lundi 23 novembre 2015

Storing the response from ajax call in ember

I am new to ember and JavaScript.I have designed a ember app and integrate the fb login in it.Now I have to send the access_token of Fb login to app backend (post request) and fetch the new the token generated by backend(django).I also need to store the new token for future call to backend.

Post request response

{
  "token": "b0fb466a6eac3ad87",
  "signup_done": true
}

controller

import Ember from 'ember'; 
/* global FB */

export default Ember.Controller.extend({ 
getLoginStatus: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.getLoginStatus(function(response) {
      if (response.status) {
        resolve(response.status);
      } else {
        reject();
      }
    });
  });
}, 
getAccessToken: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.login(function(response) {
      if (response.authResponse) {
        console.log("accesstoken>>>>>>>>>"+response.authResponse.accessToken);
        resolve(response.authResponse.accessToken);
      } else {
        reject(response);
      }
    }, { scope: 'email,user_photos,user_friends' });
  });
},

 authenticate: function(accessToken) {
    console.log("accesstoken<<<<<<<<<<<             "+accessToken);
      Ember.$.ajax({
        url: 'http://example.com/api/',
        data:{
          access_token: accessToken,
          provider:'facebook'
        },
        type: 'post',
        success:function(data){
          console.log('data>>>'+this.get(data));
        }
      });
  },

 testAPI:function() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
    });
},



   actions: {
    fblogin: function() {
      var self = this;
      this.getLoginStatus().then(function() {
        self.getAccessToken().then(function(accessToken) {
          self.authenticate(accesstoken)
            self.testAPI();
            console.log('success')
          }, function(reason) {
            console.log('failure', reason);
          });
        });

    },
    carousel:function (result) {
          var photos=result.photoThumbs
          this.set('model.photos',photos)
          console.log(this.get("model.results"));
          },
  }

});

I am able to make the ajax call to backend server and getting the response.But Not sure how to save the new token for future.




Aucun commentaire:

Enregistrer un commentaire