$user
stormpath.userService
Use this service to get the current user and do access control checks on the user.
Attempts to create a new user by submitting the given accountData as
JSON to /register. The POST endpoint can be modified via the
REGISTER_URI
config option.
| Param | Type | Details |
|---|---|---|
| accountData | Object | An object literal for passing the data to the new account. Required fields:
|
| promise | A promise representing the operation to create a
new user. If an error occurs (duplicate email, weak password), the
promise will be rejected and the http response will be passed.
If the operation is successful, the promise
will be resolved with a boolean
|
$user.create(accountData)
.then(function(account){
if(account.status === 'ENABLED'){
// The account is enabled and ready to use
}else if(account.status === 'UNVERIFIED'){
// The account requires email verification
}
})
.catch(function(err){
// Show the error message to the user
$scope.error = err.message;
});
Attempt to get the current user. Returns a promise. If the user is authenticated, the promise will be resolved with the user object. If the user is not authenticated, the promise will be rejected and passed the error response from the $http service.
If you cannot make use of the promise, you can also observe the $notLoggedin or $currentUser events. They are emitted when this method has a success or failure.
The result of this operation will be cached on the $user.currentUser property.
The user object is a Stormpath Account object, which is wrapped by a
User type. It is fetched from the /me endpoint on your
server, which is provided by our framework integrations.
| Param | Type | Details |
|---|---|---|
| bypassCache | Boolean | By default, the UserService will cache the user object after it is
retrieved the first time. Specify (default: false) |
| promise | A promise representing the operation to get the current user data. |
var myApp = angular.module('myApp', ['stormpath']);
myApp.controller('MyAppCtrl', function ($scope, $user) {
$user.get()
.then(function (user) {
console.log('The current user is', user);
})
.catch(function (error) {
console.log('Error getting user', error);
});
});
Triggers a password reset email to the given username or email address.
| Param | Type | Details |
|---|---|---|
| data | Object | An object literal for passing the email address. |
| promise | An $http promise representing the operation to generate a password reset token for the given email address. Will resolve, even if the email address does not exist. If rejected there was a network error. |
Re-sends the verification email to the account specified by the username or email address.
| Param | Type | Details |
|---|---|---|
| data | Object | An object literal for passing the username or email. |
| promise | An $http promise representing the operation to resend a verification token to the given email address. Will resolve, even if the email address does not exist. If rejected there was a network error. |
Resets a user's password, using a token that was emailed to the user.
| Param | Type | Details |
|---|---|---|
| token | String | The |
| data | Object | An object literal for passing the new password. Must follow this format: |
| promise | An $http promise representing the operation to reset the password and consume the token. If resolved the password was successfully changed, if rejected the token is invalid or the posted password does not meet the password strength rules of the directory. |
Verifies a new account, using the sptoken that was sent to the user
by email.
| Param | Type | Details |
|---|---|---|
| sptoken | String | The value of the |
| promise | An $http promise representing the operation to verify the given email verification token token. If resolved the account has been verified and can be used for login. If rejected the token is expired or has already been used. |
Verifies a password reset token that was sent to the user by email. If valid, the token can be used to reset the user's password. If not valid it means that the token has expired or has already been used.
Use this method to verify the token, before asking the user to specify a new password. If the token is invalid the user must ask for another.
| Param | Type | Details |
|---|---|---|
| sptoken | String | The |
| promise | A $http promise representing the operation to verify the given password reset token token. If resolved, the token can be used. If rejected the token cannot be used. |
Retains the result of the last call to $user.get(). This property is set after every resolution of the $user.get() promise.
If the user state is unknown (while $user.get()
is waiting to be resolved), this value is null.
If the call to $user.get() has resolved, one of the following will happen:
false.This event is broadcast when a call to $user.get() and provides the user object as the second parameter.
See the next section, the $notLoggeInEvent, for example usage.
| Param | Type | Details |
|---|---|---|
| event | Object | Angular event object. |
| user | User | The current user object. |
This event is broadcast when a call to $user.get() results in an authentication failure.
This event is useful for situations where you want to trigger the call to get the current user, but need to respond to it from some other place in your application. An example could be, during application bootstrap, you make a single call to get the current user from the run function, then react to it inside your application controller.
| Param | Type | Details |
|---|---|---|
| event | Object | Angular event object. |
var myApp = angular.module('myApp', ['stormpath']);
myApp.run(function($user){
//
// Once our app is ready to run, trigger a call to $user.get()
// We can then do other things while we wait for the result
//
$user.get();
});
myApp.controller('MyAppCtrl', function ($scope, $rootScope) {
$scope.isVisible = false; // Wait for authentication
$rootScope.$on('$notLoggedIn',function(){
$state.$go('login');
});
$rootScope.$on('$currentUser',function(e,user){
$scope.isVisible = true;
});
});
This event is broadcast when a call to $user.create() is successful. The account object is returned, and you can inspec the account's status to know if email verification is required.
| Param | Type | Details |
|---|---|---|
| event | Object | Angular event object. |
| account | account | The object of the account that was created. |