Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access token instead of session #14

Closed
cajoy opened this issue Aug 11, 2014 · 9 comments
Closed

Access token instead of session #14

cajoy opened this issue Aug 11, 2014 · 9 comments

Comments

@cajoy
Copy link

cajoy commented Aug 11, 2014

As I understand passport uses Session/Cookie for callbacks. While loopback uses AccessTokens for user validation.

Is it possible somehow to redirect using AccessToken and UserId instead of session/cookie?

Thanks

@frankcarey
Copy link
Contributor

Any answer here? Do we need to add loopback.token as well?

@frankcarey
Copy link
Contributor

I've got this working by hacking loopback-component-passport module and adding custom code in the passport.authenticate() customCallback to add params of userId and access_token to the successRedirect url. This works fine except when I tried to do it the "right way" by passing in option.customCallback() (as I discovered was an option in the loopback-component-passport code).

The issue is that when passing in the customCallback vs hacking the module, it looses the scope on res, and req variables so I loose the ability to do the same redirects the default one does. Anyone find a solution to that so I don't have to hack the module?

See http://passportjs.org/guide/authenticate/

@frankcarey
Copy link
Contributor

Is it possible somehow to redirect using AccessToken and UserId instead of session/cookie?

I've created a pull request that allows for at least overriding the defaults. The PR has some sample code for implementing a redirect with params as described.

#23

For those curious, my frontend code that grabs the params and then saves them using the angular sdk looks like this:

// Note that User is from lbServices / the autogenerated angular sdk.
angular.module('myApp')
  .controller('LoginCtrl', function ($scope, User, $location, $window) {
    // this is at a route of /login and is a page that shows a button/link that will redirect the user
    // to the proper oath provider.
    $scope.loginOauth = function(provider) {
      // Redirect the user to the api to follow the login flow of the loopback api.
      $window.location.href = "http://my.api.com/auth/" + provider;
    };
  })
  // Note that LoopBackAuth is from the lbServices as well.
  // This is at a route of /login/callback and the url will look something like..
  // /login/callback?access_token=somehash?userId=someuserid
  .controller('LoginCallbackCtrl', function ($scope, User, LoopBackAuth, $location, $window) {
    // Grab the params from the current url.
    var params = $location.search();

      // Handle response by adding properties to the LBAuth and then calling save
      LoopBackAuth.currentUserId = params.userId;
      LoopBackAuth.accessTokenId = params.access_token;
      // Note that you can also set LoopBackAuth.rememberMe which changes the storage from session to local.   

      // Saves the values to local storage.
      LoopBackAuth.save();

      // TODO we might want to grab the user info here as soon as they login.
      /*
      User.getCurrent(function(test) {
        console.log("success", test);
      },
      function(err) {
        console.log("fail", err);
      }               
      );
      */
      // Just redirect the user to the homepage once they are logged in.
      $location.url("/");
  });

@frankcarey
Copy link
Contributor

Oh, and It might be a good idea to incorporate this as a built-in option as well.

@cajoy
Copy link
Author

cajoy commented Nov 3, 2014

Absolutely agree with you - it should be integrated. And you did great job. But seems like package maintainers are very busy doing other stuffs ;)

@OwenBrotherwood
Copy link

+1 interesting
If the maintainer sparks in, then I can join any effort.
headers contra cookies/urls is AAA country. strongloop/loopback#1338 @raymondfeng

@ghost
Copy link

ghost commented May 12, 2015

+1

@aiampogi
Copy link

+1 very nice idea to make it built-in

@stale
Copy link

stale bot commented Sep 6, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants