$auth
stormpath.authService
The auth service provides methods for authenticating a user, aka "logging in" the user.
Logs the user in.
Sends the provided credential data to your backend server. The server handler should verify the credentials and return an access token, which is stored in an HTTP-only cookie.
| Param | Type | Details |
|---|---|---|
| credentialData | Object | An object literal for passing username & password, or social provider token. |
| promise | A promise that is resolved with the authentication response or error response (both are response objects from the $http service). |
myApp.controller('LoginCtrl', function ($scope, $auth, $state) {
$scope.errorMessage = null;
$scope.formData = {
username: '', // Expose to user as email/username field
password: '',
};
// Use this method with ng-submit on your form
$scope.login = function login(formData){
$auth.authenticate(formData)
.then(function(){
console.log('login success');
$state.go('home');
})
.catch(function(err){
$scope.errorMessage = err.message;
});
}
});
myApp.controller('LoginCtrl', function ($scope, $auth, $state) {
$scope.errorMessage = null;
$scope.formData = {
providerId: 'facebook', // Get access token from FB sdk login
accessToken: 'CABTmZxAZBxBADbr1l7ZCwHpjivBt9T0GZBqjQdTmgyO0OkUq37HYaBi4F23f49f5',
};
// Use this method with ng-submit on your form
$scope.login = function login(formData){
$auth.authenticate(formData)
.then(function(){
console.log('login success');
$state.go('home');
})
.catch(function(err){
$scope.errorMessage = err.message;
});
}
});
Use this method to log the user out. It triggers a request
to the /logout endpoint on the server. This will delete the cookies
that are used for authentication. The
$sessionEnd
event will be emitted after a successful logout.
| promise | A promise that is resolved when the logout request of the server is complete. |
This event is broadcast when a call to $auth.authenticate() is successful.
| Param | Type | Details |
|---|---|---|
| event | Object | Angular event object. |
| httpResponse | httpResponse | The http response from the $http service. If you are writing your access tokens to the response body when a user authenticates, you will want to use this response object to get access to that token. |
This event is broadcast when a call to $auth.endSession() is successful. Use this event when you want to do something after the user has logged out.
| Param | Type | Details |
|---|---|---|
| event | Object | Angular event object. |