Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS + OAuth

I'm trying to write a login solution for my Angular App,
This means to allow the user to connect via Facebook/Google/Twitter or Register normally.
I found Angular-OAuth to be useful, but it didn't seem to work with Facebook (or Twitter).

Anyone knows of an all-inclusive solution for this?

like image 883
Asaf Avatar asked Aug 13 '13 20:08

Asaf


People also ask

What is OAuth in angular?

OAuth2 flow. OAuth2 is just an alternative to the traditional client-server authentication model. In this model,an application requests access to a protected resource on the server ,on behalf of the user of the application and using the user's credentials.

What is OAuth in JS?

OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives. This OAuth 2.0 flow is called the implicit grant flow.

Is OAuth Authn or Authz?

OAuth is not authentication. It's an authorization protocol, or, better yet, a delegation protocol. It's for this reason that identity protocols such as OpenID Connect exist and legacy protocols such as SAML use extension grants to link authentication and delegation.


1 Answers

Here is a simple example using just redirects with angular js

Here is how to redirect to authentication

angular.module('angularoauthexampleApp')   .controller('MainCtrl', function ($scope) {     $scope.login=function() {         var client_id="your client id";         var scope="email";         var redirect_uri="http://localhost:9000";         var response_type="token";         var url="https://accounts.google.com/o/oauth2/auth?scope="+scope+"&client_id="+client_id+"&redirect_uri="+redirect_uri+         "&response_type="+response_type;         window.location.replace(url);     };   }); 

Here is how to handle the redirect after authentication

angular   .module('angularoauthexampleApp', [   ])   .config(function ($routeProvider) {     $routeProvider       .when('/access_token=:accessToken', {         template: '',         controller: function ($location,$rootScope) {           var hash = $location.path().substr(1);            var splitted = hash.split('&');           var params = {};            for (var i = 0; i < splitted.length; i++) {             var param  = splitted[i].split('=');             var key    = param[0];             var value  = param[1];             params[key] = value;             $rootScope.accesstoken=params;           }           $location.path("/about");         }       })   }); 

More complete information here http://anandsekar.github.io/oauth2-with-angularjs/

like image 132
Anand Rajasekar Avatar answered Sep 30 '22 13:09

Anand Rajasekar