简体   繁体   中英

How to get response data from the server in angularjs

This is my scenario. A server in nodejs handles the authentication procedure while in the frontend we have angularjs. when the user clicks on the button, he signs in with Facebook then the server handles all the aspects of the authentication at the end redirect to the uri of the angularjs application. We have in the server something like that

module.exports = function(request, reply) {

  if (request.auth.isAuthenticated) {

    var profile = request.auth.credentials.profile.raw;

    // set to cookie
    request.auth.session.set(profile);

    // Perform any account lookup or registration, setup local session,
    // and redirect to the application. The third-party credentials are
    // stored in request.auth.credentials. Any query parameters from
    // the initial request are passed back via request.auth.credentials.query

    // here we should redirect the app flow somewhere
    return reply({ profile: profile.id }).redirect('http://localhost:8080/app');
  }

  return reply('Unauthorized').code(401);
};

My issue is that I don't know how to retrieve profile object in angularjs. I mean I know that exist $http provider but in the following case the request doesn't start from angularjs. summering the flow the server reply with SPA if the user sign is successfully


$http.get('/app')
  .success(function(data){
     console.log(data);
   });

you can send it as an URL Parameter using the $routeProvider

your config should look like this:

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/login/:profileId', {
        templateUrl: 'template.html',
        controller: 'loginCtrl'
      }).
      otherwise({
        redirectTo: '/home'
      });
  }]);

your controller:

app.controller("loginCtrl",function($routeParams,$scope){
   $scope.profileId = $routeParams.profileId;
   //YOU CAN REDIRECT HERE TO ANOTHER VIEW AFTER
})

Back-end

module.exports = function(request, reply) {

  if (request.auth.isAuthenticated) {

    var profile = request.auth.credentials.profile.raw;

    // set to cookie
    request.auth.session.set(profile);

    // Perform any account lookup or registration, setup local session,
    // and redirect to the application. The third-party credentials are
    // stored in request.auth.credentials. Any query parameters from
    // the initial request are passed back via request.auth.credentials.query

    // here we should redirect the app flow somewhere
    return reply({ profile: profile.id
    // use # for html5 mode 
  }).redirect('http://localhost:8080/app/#/login/'+profile.id);
  }

  return reply('Unauthorized').code(401);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM