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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings