Expand description

An actix_session actix_session::storage::SessionStore for surrealdb

This crate implements the actix_session::storage::SessionStore for surrealdb allowing you to Store session state in a surrealdb database

Example

The SurrealSessionStore can be used just like any other actix_session::storage::SessionStore with the difference that it needs an already connected DBConnection

use actix_session::{config::PersistentSession, SessionMiddleware};
use actix_session_surrealdb::SurrealSessionStore;
use actix_web::{cookie::{time::Duration, Key}, App, HttpServer};
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};

#[actix_web::main]
async fn main -> io::Result<()> {
    let db = Surreal::new::<Ws>("127.0.0.1:8000").await.expect("DB to connect");

    db.signin(Root {
        username: "root",
        password: "root"
    })
    .await
    .expect("DB Credentials to be correct");

    db.use_ns("test").use_db("test").await.unwrap();

    let key = Key::generate();

    HttpServer::new(move || {
        App::new()
            .wrap(
                SessionMiddleware::builder(
                    SurrealSessionStore::from_connection(db.clone(), "sessions"),
                    key.clone()
                )
                .cookie_same_site(actix_web::cookie::SameSite::None)
                .cookie_secure(true)
                .cookie_http_only(true)
                .session_lifecycle(
                    PersistentSession::default()
                        .session_ttl_extension_policy(actix_session::config::TtlExtensionPolicy::OnStateChanges)
                        .session_ttl(Duration::days(7)),
                )
                .build(),
            )
    })
    .bind(("127.0.0.1", "8080"))?
    .run()
    .await
}

Structs

Type Aliases