Created
June 3, 2015 15:30
-
-
Save danemacaulay/f6c5084bcc33b9e8aa8d to your computer and use it in GitHub Desktop.
auto-login.js
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
watch(); | |
function login(){ | |
var username = 'username'; | |
var pass = 'password'; | |
var injector = angular.element(document.body).injector(); | |
var $http = injector.get('$http'); | |
var data = "username="+encodeURIComponent(username)+"&password="+encodeURIComponent(pass); | |
$http({ | |
method: 'POST', | |
url: '/services/authentication/login', | |
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, | |
data: data | |
}).then(function(resp){ | |
if(resp.status === 200) { | |
window.location.href = '/'; | |
} | |
else { | |
alert('login failed'); | |
} | |
}); | |
} | |
function watch() { | |
if(window.location.pathname === '/authentication/login'){ | |
login(); | |
} | |
var injector = angular.element(document.body).injector(); | |
var $rootScope = injector.get('$rootScope'); | |
$rootScope.$on('$stateChangeSuccess', function(event, toState){ | |
if(toState.name === 'login') { | |
login(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
used in conjunction with Custom Javascript Chrome extension