angular-oauth2是AngularJS的OAuth2认证模块,使用ES6编写。
使用方法:
1.引入js库:
<script src="bower_components/angular/angular.min.js"></script><script src="bower_components/angular-cookie/dist/angular-cookie.min.js"></script><script src="bower_components/query-string/query-string.min.js"></script><script src="bower_components/angular-oauth2/dist/angular-oauth2.min.js"></script>2.配置OAuth和OAuthToken
angular.module('myApp', ['angular-oauth2']) .config(['OAuthProvider', function(OAuthProvider) { OAuthProvider.configure({ baseUrl: 'https://api.website.com', clientId: 'CLIENT_ID', clientSecret: 'CLIENT_SECRET', }); }]);3.错误处理:
angular.module('myApp', ['angular-oauth2']) .run(['$rootScope', '$window', function($rootScope, $window) { $rootScope.$on('oauth:error', function(event, rejection) { // Ignore `invalid_grant` error - should be catched on `LoginController`. if ('invalid_grant' === rejection.data.error) { return; } // Redirect to `/login` with the `error_reason`. return $window.location.href = '/login?error_reason=' + rejection.data.error; }); }]);
评论