Answer the question
In order to leave comments, you need to log in
MEAN how to save the user after login?
Hello. I am learning with this stack. Started the server, installed nginx. Created two models User and Post.
On the server, authentication happens with passport
var mongoose = require('mongoose');
var User = mongoose.model('User');
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
serializeUser and deserializeUser functions.
Next comes the login and register logic. Standard that I found on the Internet :)
I use Angular 1.4.7 on the client. While there is Registration, Login and sending posts and receiving them. This is about the client. The question is that after logging in to passport, I see that the current user is hanging in the session (if I'm not confused). And in Angular, after I refresh the page, of course, it is no longer in $rootScope and $scope.
So here's the question. As I understand it, there are angular-cookies that I included. Do I need them in order to save the user in all state ? If yes, how to use them? Use as a service and return an object after saving or deleting, etc. or some other way? :)
And for the future, the question is, is there an example of how to use passport to register, for example, with Facebook, Twitter, etc. on the website of the passport is not entirely clear. I'm just a total noob at Nodejs. I'm just learning.
And the last question, explain to me where in the application should I use run? I read to dock and I can't understand what they want from me.
Thank you.
Answer the question
In order to leave comments, you need to log in
update:
It seems that while I was creating a topic, I settled with cookies, I just don’t know how true this is. At least on the client part, what about the mongo user and the user in the scope .
angular.module('authController', ['ngCookies'])
.controller('authController', ['$scope', '$http', '$rootScope', '$location', '$cookies', function($scope, $http, $rootScope, $location, $cookies){
$scope.user = {username: '', password: ''};
$scope.error_message = '';
$scope.login = function(){
$http.post('/auth/login', $scope.user).success(function(data){
if(data.state === 'success'){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$cookies.putObject('user', data.user);
$location.path('/');
}
else{
$scope.error_message = data.message;
}
});
};
}]);
})();
.run(function($rootScope, $cookies) {
$rootScope.authenticated = false;
$rootScope.current_user = '';
var cookies = $cookies.getObject('user');
if(cookies){
$rootScope.authenticated = true;
$rootScope.current_user = cookies.username;
}
$rootScope.signout = function(){
$http.get('auth/signout');
$rootScope.authenticated = false;
$rootScope.current_user = '';
};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question