AngularJS + OAuth

后端 未结 5 2091
遇见更好的自我
遇见更好的自我 2020-12-12 12:40

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

5条回答
  •  独厮守ぢ
    2020-12-12 13:10

    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/

提交回复
热议问题