cordova - Oauth 2.0 token based authentication AngularJS (Beginner) -


i have gone through multiple documents , including ng-cordova , oauth-ng still can't find resource deals basic token based authentication in angularjs/ionic

i having trouble how make curl call in angularjs

curl -x post -vu sampleapp:appkey http://sampleurl/oauth/token -h "accept: application/json" -d "password=pwd&username=sampleuname&grant_type=password&scope=read%20write&client_secret=appkey&client_id=sampleapp" 

i doing , it's giving me 401 error. however curl call works fine.

$scope.login = function() {              $http({               method: "post",                url: "http://sampleurl/oauth/token",                data:  "client_id=" + clientid + "&client_secret=" + clientsecret + "password=pwd&username=sampleuser&grant_type=password" + "&scope=read%20write",               withcredentials: true,               headers: {                 'content-type': 'application/json; charset=utf-8'                 }             })                                .success(function(data) {                     accesstoken = data.access_token;                     $location.path("/secure");                 })                 .error(function(data, status) {                     alert("error: " + data);                 });  } 

i realise that once token , have similar

$http.get('http://apiurl/api/v1/users',      {headers: { authorization: ' token api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxx'}})     .then(function(response) {             service.currentuser = response.data.user;             console.log(service.currentuser);     }); 

but far i've been unable figure out way make call server , save access token in localstorage. resources on internet catered towards 3rd party logins (google,facebook,twitter etc ) or jwt tokens.

i new @ i've found out need worry password grant flow user gives his/her credentials consumer , consumer exchanges these access , refresh token. still don't believe making right call.

update : @danielcottone in answer below has mentioned , oauth-ng seemed solution documentation i've seen confuses me want send username , password url , sample not implementing or has provision can tell?

this have in documentation :

<oauth   site="http://oauth-ng-server.herokuapp.com"   client-id="d6d2b510d18471d2e22aa202216e86c42beac80f9a6ac2da505dcb79c7b2fd99"   redirect-uri="http://localhost:9000"   profile-uri="http://oauth-ng-server.herokuapp.com/api/v1/me"   scope="public"> </oauth> 

again , first time i'm trying integration of kind , makes sense me think call have credentials sent it? how send ?

the best way solve storing token in localstorage after authentication, , using interceptor inject token request headers:

$http authentication promise (you need inject $localstorage)

.success(function(data) {   $localstorage.accesstoken = data.access_token;   $location.path("/secure"); }) 

authentication interceptor

.factory('authinterceptor', function ($q, $localstorage, $rootscope) {    return {     request: function (config) {       if ($localstorage.access_token) {         config.headers['authorization'] = 'token api_key=' + $localstorage.token;       }       return config;     },      responseerror: function (response) {       if (response.status === 401 || response.status === 403) {         delete $localstorage.access_token;         // kind of redirect login page here...       }       return $q.reject(response);     }   }; }); 

to logout, delete token localstorage, , further requests redirected login page if 401 or 403 api.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -