Reputation: 2711
I'm trying to create an authentication-service in angular in order to register/login users.
After registering a new user I have some trouble receiving the response to the service.
Here is the flow:
Front-end controller that passes the new user to the Auth-service:
vm.onSubmit = function () {
Authentication
.register(vm.credentials)
.then(function(){
$location.path('/');
});
};
The Authentication-services register
-function
register: function(user) {
return $http.post('/api/register', user).success(function(data){
console.log('authserviceDONE'); //I never get back here...
});
},
The api/register
-route
app.post('/api/register', function(req, res) {
User.register(new User({ username: req.body.username}), req.body.password, function(err,user) {
if (err) {
return res.json(err);
}
passport.authenticate('local')(req, res, function () {
console.log(req.user); // User gets logged
return req.user;
});
});
});
I am hoping to receive the user-object in the front-end in order to log him in to the app but the Auth-service never receives anything. Any tips on what I might be missing here?
Upvotes: 1
Views: 1222
Reputation: 348
I do not know how your project is structured so I am going to show you what works for me. I usually have three files: app.js
controllers.js
services.js
- this is pretty standard.
Here is an example:
app.js
angular.module('myApp', ['myApp.controllers', 'myApp.services'])
.config(function (...) { ...
controllers.js
angular.module('myApp.controllers', [])
.controller('myCtrl',function($scope, srvAccount, $log){
srvAccount.getaccount()
.then(
function(response){
$scope.account = response.data;
$log.info(response.data);
},
function(rejection){
$log.error("getaccount error");
$log.log(rejection);
}
);
...
and finally services.js
angular.module('myApp.services', [])
.factory('srvAccount', function ($http) {
return {
getaccount: function () {
return $http({
method: 'GET',
url: 'api/getaccount'
});
}
}
});
Have the browser developer tools > console open and use AngularJS $log
to debug. More on $log
here https://docs.angularjs.org/api/ng/service/$log
Upvotes: 1