Created
March 15, 2016 21:39
-
-
Save irenelfeng/cf06a07f17574e49e0c5 to your computer and use it in GitHub Desktop.
a login button directive in Angular.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<login redirect="/newpath">Bind to App Failed</login> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// depends on the authentication.js and login.js files | |
//authentication to give the key for OAuth login.js does other things like caching the data from Oauth, and other things if need be. | |
angular.module('app.loginDirective', []) | |
.directive('login', ['authentication','login','$http','$location', function(authentication, $http, $location) { | |
var Template = '<div><button type="button" class="btn btn-warning" ng-click="login()">Login</button><p ng-show="uploadError" class="help-block">{{ uploadError }}</p></div>'; | |
return { | |
restrict: 'E', | |
scope: { | |
redirect: '@', | |
uploadError: '&' | |
}, | |
template: Template, | |
replace: true, | |
link: function(scope, element, attr){ | |
scope.login = function(){ | |
// will log in | |
OAuth.initialize(authentication.getOAuthKey()); | |
OAuth.popup('google', function(error, result) { | |
$http | |
.get('api/login',{params:{token:result.access_token}}) | |
.success(function (data, status, headers, config) { | |
login.login(data); | |
// redirects to the path specified in the html | |
$location.path(scope.redirect); | |
} | |
}) | |
.error(function(error){ | |
scope.uploadError = "Unauthorized login, please try again"; | |
}); | |
}); | |
}; | |
} | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment