angularjs - Lose cookie/session when reload page -
i have 2 separate applications:
- angularjs client working on http://localhost:8080
- nodejs api server working on http://localhost:3000
nodejs api server gives json data client (and not give html pages).
i store information autentificated user in sessionstorage.
the problem: when login system , reload page, lost information user in sessionstorage.
here angularjs auth service:
module.exports = function($http, $location, $cookies, $alert, $window) { $window.sessionstorage.setitem('currentuser', json.stringify($cookies.get('user'))); $cookies.remove('user'); return { login: function(user) { return $http.post('http://localhost:3000/api/login', user).success(function(data) { $window.sessionstorage.setitem('currentuser', json.stringify(data)); $location.path('/fillprofile'); $alert({ title: 'cheers!', content: 'you have logged in.', placement: 'top-right', type: 'success', duration: 3 }); }).error(function() { $alert({ title: 'error!', content: 'invalid username or password.', placement: 'top-right', type: 'danger', duration: 3 }); }); }, signup: function(user) { return $http.post('http://localhost:3000/api/signup', user).success(function(data) { $location.path('/login'); $alert({ title: 'congratulations!', content: 'your account has been created.', placement: 'top-right', type: 'success', duration: 3 }); }).error(function(response) { $alert({ title: 'error!', content: response.data, placement: 'top-right', type: 'danger', duration: 3 }); }); }, logout: function() { return $http.get('http://localhost:3000/api/logout').success(function() { $window.sessionstorage.setitem('currentuser', null); $cookies.remove('user'); $location.path('/login'); $alert({ content: 'you have been logged out.', placement: 'top-right', type: 'info', duration: 3 }); }); } }; };
nodejs api
app.use(function(req, res, next) { if (req.user) { res.cookie('user', json.stringify(req.user)); } next(); }); function ensureauthenticated(req, res, next) { if (req.isauthenticated()) next(); else res.send(401); } passport.serializeuser(function(user, done) { done(null, user.id); }); passport.deserializeuser(function(id, done) { user.findbyid(id, function(err, user) { done(err, user); }); }); passport.use(new localstrategy(function(username, password, done) { user.findone({ username: username }, function(err, user) { if (err) return done(err); if (!user) return done(null, false); user.comparepassword(password, function(err, ismatch) { if (err) return done(err); if (ismatch) return done(null, user); return done(null, false); }); }); }));
routes
app.post('/api/login', passport.authenticate('local'), function(req, res) { res.cookie('user', json.stringify(req.user)); res.send(req.user); }); app.post('/api/signup', function(req, res, next) { var user = new user({ username: req.body.username, email: req.body.email, password: req.body.password }); user.save(function(err) { if (err) return next(err); res.send(200); }); }); app.get('/api/logout', function(req, res, next) { req.logout(); res.send(200); });
try removing $cookies.remove('user');
on line 3
Comments
Post a Comment