AngularJS - get user's access token

426 views
Skip to first unread message

Nolan Dubeau

unread,
Mar 17, 2015, 11:53:35 AM3/17/15
to loopb...@googlegroups.com
Hi,

I'm using a third-party file uploader directive in AngularJS to post a photo to our loopback API and I need to get access to the logged in user's access token in AngularJS in order to authenticate the request.  Do the generated services have a utility function to retrieve the current user's access token?  It doesn't appear that there is.  This would be a great feature.

Thanks.

Nolan

Tim Geerts

unread,
Mar 17, 2015, 4:29:55 PM3/17/15
to loopb...@googlegroups.com
I'm very new to the angularjs + loopback stack, but am having loads of issues with authorisation in requests (after a user is logged in).
But, in the very least, this might help you, inject LoopBackAuth into the controller/factory you want to have the accesstoken, do a console.log of that object, but I think it's just LoopBackAuth.Token or something similar.

Daryl Rodrigo

unread,
Mar 18, 2015, 3:27:32 PM3/18/15
to loopb...@googlegroups.com
I'm actually also really confused about this.

I managed to get the accessToken from when you look in - and i know this gets stored in local storage. But what do you do with it then? I can manage the sessions by holding a variable in my angular app but when you refresh the page this is obviously all lost. 

Is there a way to do this with the Angular SDK?

Joan Barros

unread,
Mar 14, 2016, 10:25:11 PM3/14/16
to LoopbackJS
A bit old but for the record.

There's a provider called LoopBackAuth with a property called accessTokenId. You can get the access token doing something like this:

angular.module().controller('TestC', ['LoopBackAuth', function (LoopBackAuth) {
  console.log(LoopBackAuth.accessTokenId);
}]);

Hope this helps someone. I had to dive into the code for the SDK in order to find this.

tamilve...@gmail.com

unread,
Mar 15, 2016, 4:58:42 AM3/15/16
to LoopbackJS

You will only need the access token when you try to make request on your own(ie: by, not using the loopback angular model method.). Otherwise, access token will be automatically added in your requests. You dont need to cache them in your code. I hope this helps. 
Reply all
Reply to author
Forward
0 new messages