Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

No9
Copy link
Collaborator

@No9 No9 commented Oct 10, 2020

This patch fixes #6 and also exposes a datetime property named created at the root level of the session document as using the expiry property inside the session store object gives this error.

db.collection.createIndex( { "session" : { "expiry": 1 } }, { expireAfterSeconds: 3600 } ) 
{
	"ok" : 0,
	"errmsg" : "Values in v:2 index key pattern cannot be of type object. Only numbers > 0, numbers < 0, and strings are allowed.",
	"code" : 67,
	"codeName" : "CannotCreateIndex"
}

It also provides instructions on how to configure mongo to expire the session.

src/lib.rs Outdated
None => Ok(None),
Some(doc) => Ok(Some(serde_json::from_str(doc.get_str("session")?)?)),
Some(doc) => Ok(Some(
bson::from_bson::<Session>(doc.get("session").unwrap().clone()).unwrap(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably should be ? instead of unwrap — if something goes wrong with the data state we wouldn't want to panic the server

Cargo.toml Outdated
async-session = "2.0.0"
async-trait = "0.1.36"
serde_json = "1.0.56"
chrono = "0.4.19"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async-session reexports chrono, so this dependency is unnecessary

src/lib.rs Outdated

use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use chrono::Utc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use chrono::Utc;
use async_session::chrono::Utc;

@No9
Copy link
Collaborator Author

No9 commented Oct 11, 2020

Thanks @jbr I've removed the external chrono link as you suggested.
If I tried to apply the ? approach it raised a result mismatch as doc.get("session") is an option.
So I have handled the option explicitly and return an new session if the bson conversion fails

@jbr
Copy link
Member

jbr commented Oct 11, 2020

@No9 I don't think building a new session is ever in the responsibilities of a session store. If it can't parse a session from the stored record, that should probably be an Err with some sort of synthetic error that describes the problem

@jbr jbr mentioned this pull request Oct 11, 2020
@No9
Copy link
Collaborator Author

No9 commented Oct 11, 2020

Hey @jbr
I think return Ok(None) is the right semantics as the actual session sub document doesn't exist.
The error from parsing of the session is handled in the bson::from_bson line
https://github.com/yoshuawuyts/async-mongodb-session/pull/9/files#diff-b4aea3e418ccdb71239b96952d9cddb6R103
And that will return an error.

@pepoviola pepoviola mentioned this pull request Oct 11, 2020
Thanks very much for the work on this @pepoviola - I like where this has landed with a simple session object that can be inlined with in the SessionMiddleware creation but also provides advanced options available.
@No9
Copy link
Collaborator Author

No9 commented Dec 21, 2020

Closing as this was integrated into #11

@No9 No9 closed this Dec 21, 2020
@No9 No9 reopened this Dec 21, 2020
@No9 No9 merged commit d0ef823 into master Dec 21, 2020
@delete-merged-branch delete-merged-branch bot deleted the session-as-object branch December 21, 2020 18:28
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

Successfully merging this pull request may close these issues.

session should be stored as an object instead of a string
3 participants