ExpressJS Session Management
up vote
0
down vote
favorite
So for the longest time I could not seem to figure out how to manage sessions in Express. Yes, I've looked everywhere on YouTube, Stack Over Flow, etc. and it just doesn't make any sense to me at all.
So I know the basics, installing the express-sessions module and then requiring it, and then registering the middleware in our application. Now we have access to the session object via request object by doing request.session. Ok, but now what?
I think the biggest problem I'm having is how to actually maintain the user's session. In my recent application, I have a registration and login page. Once a user registers, they are prompted to login. When the user logs in the app, on the back-end, I first check if the provided details match the database, if it does, then that's where I reference request.session object and do some stuff there. For example:
app.get('/login', (req, res) => {
// Get user login details.
var userSession = {
user: req.body.username,
sessID: req.sessionID
}
// Store the user session somewhere, either in a Session Store.
// or a database. For now I will store it in an array.
sessionArray.push(userSession);
});
Okay, so now our array will have a user session. But now what? Every time the user tries to access a route, I need to check the user session right? That shouldn't be too hard. But what about if we have different origins of the application? For example, my VueJS application is on port 8080, and my Express server is on port 3000. I would send an AJAX call from the Vue app every time I try to access a route on the Vue app, and check the server if the session/username exists inside whatever data structure/storage we stored our session in, and if it is there, we send back a result indicating the user is logged in and good to go.
Is that all there is to it? This is the hardest thing I've came across so far.
node.js http express session cookies
add a comment |
up vote
0
down vote
favorite
So for the longest time I could not seem to figure out how to manage sessions in Express. Yes, I've looked everywhere on YouTube, Stack Over Flow, etc. and it just doesn't make any sense to me at all.
So I know the basics, installing the express-sessions module and then requiring it, and then registering the middleware in our application. Now we have access to the session object via request object by doing request.session. Ok, but now what?
I think the biggest problem I'm having is how to actually maintain the user's session. In my recent application, I have a registration and login page. Once a user registers, they are prompted to login. When the user logs in the app, on the back-end, I first check if the provided details match the database, if it does, then that's where I reference request.session object and do some stuff there. For example:
app.get('/login', (req, res) => {
// Get user login details.
var userSession = {
user: req.body.username,
sessID: req.sessionID
}
// Store the user session somewhere, either in a Session Store.
// or a database. For now I will store it in an array.
sessionArray.push(userSession);
});
Okay, so now our array will have a user session. But now what? Every time the user tries to access a route, I need to check the user session right? That shouldn't be too hard. But what about if we have different origins of the application? For example, my VueJS application is on port 8080, and my Express server is on port 3000. I would send an AJAX call from the Vue app every time I try to access a route on the Vue app, and check the server if the session/username exists inside whatever data structure/storage we stored our session in, and if it is there, we send back a result indicating the user is logged in and good to go.
Is that all there is to it? This is the hardest thing I've came across so far.
node.js http express session cookies
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So for the longest time I could not seem to figure out how to manage sessions in Express. Yes, I've looked everywhere on YouTube, Stack Over Flow, etc. and it just doesn't make any sense to me at all.
So I know the basics, installing the express-sessions module and then requiring it, and then registering the middleware in our application. Now we have access to the session object via request object by doing request.session. Ok, but now what?
I think the biggest problem I'm having is how to actually maintain the user's session. In my recent application, I have a registration and login page. Once a user registers, they are prompted to login. When the user logs in the app, on the back-end, I first check if the provided details match the database, if it does, then that's where I reference request.session object and do some stuff there. For example:
app.get('/login', (req, res) => {
// Get user login details.
var userSession = {
user: req.body.username,
sessID: req.sessionID
}
// Store the user session somewhere, either in a Session Store.
// or a database. For now I will store it in an array.
sessionArray.push(userSession);
});
Okay, so now our array will have a user session. But now what? Every time the user tries to access a route, I need to check the user session right? That shouldn't be too hard. But what about if we have different origins of the application? For example, my VueJS application is on port 8080, and my Express server is on port 3000. I would send an AJAX call from the Vue app every time I try to access a route on the Vue app, and check the server if the session/username exists inside whatever data structure/storage we stored our session in, and if it is there, we send back a result indicating the user is logged in and good to go.
Is that all there is to it? This is the hardest thing I've came across so far.
node.js http express session cookies
So for the longest time I could not seem to figure out how to manage sessions in Express. Yes, I've looked everywhere on YouTube, Stack Over Flow, etc. and it just doesn't make any sense to me at all.
So I know the basics, installing the express-sessions module and then requiring it, and then registering the middleware in our application. Now we have access to the session object via request object by doing request.session. Ok, but now what?
I think the biggest problem I'm having is how to actually maintain the user's session. In my recent application, I have a registration and login page. Once a user registers, they are prompted to login. When the user logs in the app, on the back-end, I first check if the provided details match the database, if it does, then that's where I reference request.session object and do some stuff there. For example:
app.get('/login', (req, res) => {
// Get user login details.
var userSession = {
user: req.body.username,
sessID: req.sessionID
}
// Store the user session somewhere, either in a Session Store.
// or a database. For now I will store it in an array.
sessionArray.push(userSession);
});
Okay, so now our array will have a user session. But now what? Every time the user tries to access a route, I need to check the user session right? That shouldn't be too hard. But what about if we have different origins of the application? For example, my VueJS application is on port 8080, and my Express server is on port 3000. I would send an AJAX call from the Vue app every time I try to access a route on the Vue app, and check the server if the session/username exists inside whatever data structure/storage we stored our session in, and if it is there, we send back a result indicating the user is logged in and good to go.
Is that all there is to it? This is the hardest thing I've came across so far.
node.js http express session cookies
node.js http express session cookies
asked Nov 7 at 18:04
Stuy Foong
1
1
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31
add a comment |
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195220%2fexpressjs-session-management%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have to get session data from express server and save it in localstorage of your front end e.g. window.localStorage.setItem('session', sessiondata), so you could have access to it whenever you need just doing userSession = window.localStorage.getItem('session'), then when logout just call localStorage.clear(). Hope it helps.
– vitomadio
Nov 7 at 18:31