Skip to content

Fix store_session impl #4

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

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use mongodb::bson::doc;
use mongodb::options::ReplaceOptions;
use mongodb::Client;

/// A MongoDB session store.
Expand Down Expand Up @@ -48,19 +49,19 @@ impl SessionStore for MongodbSessionStore {
// TODO: mongodb supports TTL for auto-expiry somehow, need to figure out how!
let value = serde_json::to_string(&session)?;
let id = session.id();
let doc = doc! { "session_id": id, "session": value };
coll.insert_one(doc, None).await?;
let query = doc! { "session_id": id };
let replacement = doc! { "session_id": id, "session": value };
let opts = ReplaceOptions::builder().upsert(true).build();
coll.replace_one(query, replacement, Some(opts)).await?;

Ok(session.into_cookie_value())
}

async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
let id = Session::id_from_cookie_value(&cookie_value)?;
let coll = self.client.database(&self.db).collection(&self.coll_name);

let filter = doc! { "session_id": id };
let result = coll.find_one(filter, None).await?;
match result {
match coll.find_one(filter, None).await? {
None => Ok(None),
Some(doc) => Ok(Some(serde_json::from_str(doc.get_str("session")?)?)),
}
Expand Down