Giter VIP home page Giter VIP logo

Comments (4)

joewagner avatar joewagner commented on June 13, 2024 1
  • Does the session.reload() function saves after its execution just like doing session.reload() and session.save() ? (If yes I could optimize and fix a lot of bugs)

reload loads the req.session object with whatever value is in your store. It does not save the current session object.
I don't fully understand what your code is trying to accomplish, but this middleware automatically loads the session for you. So if you can try to change your code so you don't reload the session, that might be a general way to start eliminating bugs.

  • Is there any like deep documentation of express-session which explains in detail what each function does ? (So I could know what issues my code has, and if some function already saves by themselves)

The README for this repo is most detailed documentation I know of.
The loading and saving of the session should generally "just work". The only time I've found myself needing to intervene in the loading and saving is when I modify the session inside a route that results in a redirect, which does not appear to be the case for you.

from session.

TheDogHusky avatar TheDogHusky commented on June 13, 2024 1

All right, thank you very much for your answers, everything’s clear now. I’ll look into fixing my code and clearing things up.
As for my code, I am trying to update the session each time someone makes a request to the website from a browser: check if the token is still valid (if not logout the user), if he is still in the database (if not, log him out to make him login back so it adds a valid entry).

The overall thing I’m trying to make with zero experience in sessions is a discord community website where there are accounts from Discord login, with a database to store account related data, and a lot of functionalities including an admin dashboard with statistics, ticket management, announcement creation, partnership management, account management (with different permissions and access following the user’s staffLevel variable).
So then partnerships are shown on a special page, announcements too and in the main page, etc..

Mostly a paid project one of my friends wants me to do to train myself and make him a good website (so you don’t punch me in the face for being that incompetent in a paid job xD)

That is a big project and I’m paid to do it, but it’s my first time making things that complex with sessions.

Anyways, you helped me on the issue I had, I am not going to bother you with the overall thing as I already know it’s horribly unoptimized and ugly.

Thank you so much for your time and help,
Adam.

from session.

joewagner avatar joewagner commented on June 13, 2024

@TheDogHusky I can't say for sure what the problem is, but it looks like your updateSession function promise is calling session.reload, and session.save at the same time. If you want to reload the session here, you should probably wait to modify and save to the session until after the reload finishes.

something like:

export function updateSession(req: Request, res: Response, isDash?: boolean, redirectTo?: boolean): Promise<void|FlashMessage[]> {
    return new Promise(async (resolve, reject) => {
        await new Promise(function (resolve, reject) {
            req.session.reload((err: Error) => {
                if(err) {
                    console.log(err); // this might help debugging
                    updateSession(req, res, isDash, redirectTo).then(resolve).catch(reject);
                } else {
                    resolve();
                }
           });
        });

       // now your session is reloaded...

from session.

TheDogHusky avatar TheDogHusky commented on June 13, 2024

I’ll try your solution out, thank you very much.

As I don’t know express-session a lot, I just have some little questions about this code:

  • Does the session.reload() function saves after its execution just like doing session.reload() and session.save() ? (If yes I could optimize and fix a lot of bugs)
  • Is there any like deep documentation of express-session which explains in detail what each function does ? (So I could know what issues my code has, and if some function already saves by themselves)

from session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.