-
Notifications
You must be signed in to change notification settings - Fork 6
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
Comments
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: |
"Error: SessionEngine: Session was not started. Please call Request.session.start() before calling Request.session.get()" Can I have an example? |
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
}); |
How do I retrieve the "is_logged_in" value in other files? |
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
}
}); |
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? |
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 |
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. |
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 |
It is working now, thank you sooo much ❤️ |
There is no way to check if the user is logged in
The text was updated successfully, but these errors were encountered: