As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
I need the login function that need to return either the activeUser object or error from kinvey api so my controller can display whether login is succesfully or failure.
I always got result Object {activeUser : null, error: null}. Would someone help me to verify whether my implementation is correct?
Below is the code
//UserService.js
`
.factory('UserService', function ($kinvey) {
return {
login: function (_username, _password) {
var result = null;
var currentUser = null;
var sysError = null;
var promise = $kinvey.init({
appKey: 'kid_bkzp2kz_SA',
appSecret: 'c9293f32c3f44340a690f5f846259168',
sync: { enable: false }
});
//try to initial first
promise.then(function () {
console.log("Kinvey initial completed")
//if initial complete, try to logout the current user
return result = { activeUser : currentUser, error: sysError};
}
};
};
`
//LoginController.js
`
.controller('LoginController',
['$scope', '$kinvey', "$state", function ($scope, $kinvey, $state, UserService) {
$scope.signIn = function (user) {
console.log('Sign-In', user);
var result = UserService.login(user.username, user.password);
console.log (result);
//I always got result Object {activeUser : null, error: null}
};
}])
`
1 Comment
P
Pete
said
over 8 years ago
Hi @ttran,
The problems you have are...
* At the time you log out the result the login operation hasn't finished
* Even if the login operation had finished, the values in the pojo you are returning will not have been updated
I've cleaned up your code and hopefully that will make things a bit clearer to you...
```
// LoginController.js
Angular.controller('LoginController', function ($scope, $kinvey, UserService) {
$scope.signIn = function (user) {
UserService
.login(user.username, user.password)
.then(function(result) {
console.info('logged in', result);
}, function(error) {
console.error('errored', error);
});
});
```
```
// UserService.js
Angular.factory('UserService', function ($kinvey, $q) {
return {
login: function (_username, _password) {
// returns a promise
// * resolves with successfully logged in user object
// * rejects with error from Kinvey
//
// 1. initialise then...
// 2. logout then...
// 3. login
return $kinvey
.init({
appKey: 'kid_bkzp2kz_SA',
appSecret: 'c9293f32c3f44340a690f5f846259168',
sync: { enable: false }
})
.then(function () {
return $kinvey.User.logout();
})
.then(function () {
return $kinvey.User.login({
username: _username,
password: _password
});
})
}
};
};
```
The `UserService` object has one method `login` that returns a promise. Inside it chains three promises together to perform all the operations you need. Any errors that occur interrupt the entire chain and propagate along to the first failure block they find.
The `LoginController` object calls the `UserService.login` method and chains it's logging onto the promise that is returned.
Thanks
ttran
I need the login function that need to return either the activeUser object or error from kinvey api so my controller can display whether login is succesfully or failure.
I always got result Object {activeUser : null, error: null}. Would someone help me to verify whether my implementation is correct?
Below is the code
//UserService.js
`
.factory('UserService', function ($kinvey) {
return {
login: function (_username, _password) {
var result = null;
var currentUser = null;
var sysError = null;
var promise = $kinvey.init({
appKey: 'kid_bkzp2kz_SA',
appSecret: 'c9293f32c3f44340a690f5f846259168',
sync: { enable: false }
});
//try to initial first
promise.then(function () {
console.log("Kinvey initial completed")
//if initial complete, try to logout the current user
return $kinvey.User.logout();
console.log("User is logout ...")
}, function (error) {
console.log("Error initial " + error.description);
sysError = error;
})
.then(function () {
console.log("User logged out succesfully")
$kinvey.setActiveUser(null);
//Try to log user in
return $kinvey.User.login({
username: _username,
password: _password
});
}, function (error) {
console.log("Error logout " + error.description);
sysError = error;
})
.then(function (user) {
console.log("Logged to kinvey succesfully");
console.log("User from kinvey " + user);
currentUser = user;
}, function (error) {
console.log("Error login " + error.description);
sysError = error;
});
return result = { activeUser : currentUser, error: sysError};
}
};
};
`
//LoginController.js
`
.controller('LoginController',
['$scope', '$kinvey', "$state", function ($scope, $kinvey, $state, UserService) {
$scope.signIn = function (user) {
console.log('Sign-In', user);
var result = UserService.login(user.username, user.password);
console.log (result);
//I always got result Object {activeUser : null, error: null}
};
}])
`