Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to check if user is logged in #9

Closed
ghost opened this issue Nov 16, 2022 · 10 comments
Closed

No way to check if user is logged in #9

ghost opened this issue Nov 16, 2022 · 10 comments

Comments

@ghost
Copy link

ghost commented Nov 16, 2022

There is no way to check if the user is logged in

@kartikk221
Copy link
Owner

Yes, there is. You can store a boolean value like "logged_in" in your session and then check it on requests to determine if someone is logged in.

Simply put:
Start Session -> Check For Login Flag

@ghost
Copy link
Author

ghost commented Nov 18, 2022

"Error: SessionEngine: Session was not started. Please call Request.session.start() before calling Request.session.get()"

Can I have an example?

@kartikk221
Copy link
Owner

webserver.post('/api/login', async (request, response) => {
    // Start the session
    await request.session.start();

    // Retrieve the flag or whatever property you use to specify if someone is logged in
    const is_logged_in = request.session.get('logged_in') === true;

   // Do stuff with is_logged_in
});

@ghost
Copy link
Author

ghost commented Nov 18, 2022

How do I retrieve the "is_logged_in" value in other files?
Like express-session.

@kartikk221
Copy link
Owner

is_logged_in is not a property by default.

webserver.post('/api/login', async (request, response) => {
    // Assume you did validation and are ready to create their session, create it like below

    // Start the session
    await request.session.start();

   // Store session data (This is where you would store a flag / propery like is_logged_in)
   request.session.set({
        is_logged_in: true,
        account: 'something',
       something: 'something else'
    });

    // Send the response as you normally would
});

// Assume this is a authenticated endpoint
webserver.get('/api/account', async (request, response) => {
        // Start the session
        await request.session.start();

        // Retrieve the session data and check for is_logged_in property
        const session = request.session.get();
        if (session.is_logged_in) {
             // Do stuff here and return the account info and stuff
        }
});

@ghost
Copy link
Author

ghost commented Nov 18, 2022

When you fetch at /api/account while your logged in, it will remove all the session data because it starts the session again, which resets the session.

Any way to get around that?

@kartikk221
Copy link
Owner

No, starting a session just means the underlying session object is initialized with data. It should not reset your session. If your session is begin reset every time you start it then that means you haven't properly configured the session engine to work with your storage mechanism properly.

Please see the examples on https://github.com/kartikk221/hyper-express-session/blob/main/docs/Examples.md for a better idea of how to integrate hyper-express-session

@ghost
Copy link
Author

ghost commented Nov 18, 2022

Which Redis package are you using for https://github.com/kartikk221/hyper-express-session/blob/main/docs/Examples.md?

Cause the package I am using seems to have issues.

@kartikk221
Copy link
Owner

That example is based on https://github.com/luin/ioredis but the session engine should work with any database and any adapter as long as you properly reflect each of the engine's actions into your db

@ghost
Copy link
Author

ghost commented Nov 18, 2022

It is working now, thank you sooo much ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant