Skip to main content

Session

Struct Session 

Source
pub struct Session<T>
where T: DatabasePool + Clone + Debug + Sync + Send + 'static,
{ pub(crate) store: SessionStore<T>, pub(crate) id: String, }
Expand description

A Session Store.

Provides a Storage Handler to SessionStore and contains the ID of the current session.

This is Auto generated by the Session Layer Upon Service Execution.

Fieldsยง

ยงstore: SessionStore<T>

The SessionStore that holds all the Sessions.

ยงid: String

The Sessions current ID for looking up its store.

Implementationsยง

Sourceยง

impl<S> Session<S>
where S: DatabasePool + Clone + Debug + Sync + Send + 'static,

Source

pub(crate) async fn new( store: SessionStore<S>, value: Option<String>, ) -> Result<(Self, bool), SessionError>

Source

pub(crate) async fn generate_id( store: &SessionStore<S>, ) -> Result<String, SessionError>

Available on crate feature key-store only.
Source

pub fn create_data(&self)

Sets the Session to create the SessionData based on the current Session ID. You can only use this if SessionMode::Manual is set or it will Panic. This will also set the store to true similar to session.set_store(true);

ยงExamples
โ“˜
session.create_data();
Source

pub fn data_exists(&self) -> bool

Checks if the SessionData was created or not.

ยงExamples
โ“˜
if session.data_exists() {
    println!("data Exists");
}
Source

pub fn renew(&self)

Set session flags to renew/regenerate the ID. This deletes data from the database keyed with the old ID. This helps to enhance security when logging into secure areas on a website. The current sessionโ€™s data will be stored with the new ID.

ยงExamples
โ“˜
session.renew();
Source

pub fn update(&self)

Sets the Session to force update the database. This will increase the Timer on the sessions store making the session live longer in the persistent database.

ยงExamples
โ“˜
session.renew();
Source

pub fn destroy(&self)

Sets the Current Session to be Destroyed. This will Deleted the Session and Cookies upon Response Phase.

ยงExamples
โ“˜
session.destroy();
Source

pub fn set_longterm(&self, longterm: bool)

Sets the Current Session to a long term expiration. Useful for Remember Me setups. This will also update the database on Response Phase.

ยงExamples
โ“˜
session.set_longterm(true);
Source

pub fn set_store(&self, can_store: bool)

Allows the Current Session to store. This will also update the database on Response Phase.

This is only used when SessionMode is Manual or OptIn. This will allow the Session to be stored if true. This will delete and not allow a session to be stored if false.

ยงExamples
โ“˜
session.set_store(true);
Source

pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Gets data from the Sessionโ€™s HashMap

Provides an Option<T> that returns the requested data from the Sessions store. Returns None if Key does not exist or if serde_json failed to deserialize.

ยงExamples
โ“˜
let id = session.get("user-id").unwrap_or(0);

Used to get data stored within SessionDataโ€™s hashmap from a key value.

Source

pub fn get_remove<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Removes a Key from the Current Sessionโ€™s HashMap returning it.

Provides an Option<T> that returns the requested data from the Sessions store. Returns None if Key does not exist or if serde_json failed to deserialize.

ยงExamples
โ“˜
let id = session.get_remove("user-id").unwrap_or(0);

Used to get data stored within SessionDataโ€™s hashmap from a key value.

Source

pub fn set(&self, key: &str, value: impl Serialize)

Sets data to the Current Sessionโ€™s HashMap. This will also update the database on Response Phase.

ยงExamples
โ“˜
session.set("user-id", 1);
Source

pub fn remove(&self, key: &str)

Removes a Key from the Current Sessionโ€™s HashMap. Does not process the String into a Type, Just removes it. This will also update the database on Response Phase.

ยงExamples
โ“˜
let _ = session.remove("user-id");
Source

pub fn clear(&self)

Clears all data from the Current Sessionโ€™s HashMap instantly. This will also update the database on Response Phase.

ยงExamples
โ“˜
session.clear();
Source

pub async fn count(&self) -> i64

Returns a i64 count of how many Sessions exist.

If the Session is persistent it will return all sessions within the database. If the Session is not persistent it will return a count within SessionStore.

ยงExamples
โ“˜
let count = session.count().await;
Source

pub fn get_session_id(&self) -> String

Returns the SessionID for this Session.

ยงExamples
โ“˜
let session_id = session.get_session_id();
Source

pub fn get_store(&self) -> &SessionStore<S>

Returns the store for this Session.

The store contains everything that all sessions need.

ยงExamples
โ“˜
let store = session.get_store();
Source

pub fn get_mut_store(&mut self) -> &mut SessionStore<S>

Returns a mutable store for this Session.

The store contains everything that all sessions need.

ยงExamples
โ“˜
let store = session.get_store_mut();
Source

pub(crate) fn remove_request(&self)

Removes a Request from the request counter used to determine if parallel requests exist. prevents data deletion until requests == 0.

ยงExamples
โ“˜
session.remove_request();
Source

pub(crate) fn set_request(&self)

Removes a Request from the request counter used to determine if parallel requests exist. prevents data deletion until requests == 0.

ยงExamples
โ“˜
session.set_request();
Source

pub(crate) fn is_parallel(&self) -> bool

checks if a session has more than one request.

ยงExamples
โ“˜
session.is_parallel();
Source

pub fn verify(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

checks if a session exists and if it is outdated.

ยงExamples
โ“˜
session.verify();
Source

pub fn update_database_expires(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

Updates the sessions stored database expire time. Use this before forcing a update to the database store. will update the database expires based on if the session is longterm then configs max_lifespan. if not then configs lifespan.

THIS WILL NOT UPDATE THE DATABASE SIDE.

ยงExamples
โ“˜
session.update_database_expires();
Source

pub fn update_memory_expires(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

Updates the Sessions In memory auto remove timer. Will prevent it from being removed for the configs set memory_lifespan.

ยงExamples
โ“˜
session.update_memory_expires();
Source

pub async fn force_database_update(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

forces a update to the databases stored data for the session. Make sure to update the databases expire time before running this or the data could be unloaded by a request checking for outdated sessions.

ยงExamples
โ“˜
session.force_database_update().await;
Source

pub fn memory_remove_session(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

Removes the session from the memory store if it is not parallel. If it is parallel then each parallel session will need to call this once. when all parallel sessions are dead this gets unloaded.

THIS DOES NOT CLEAR THE KEY STORE.

ยงExamples
โ“˜
session.memory_remove_session();
Source

pub async fn database_remove_session(&self) -> Result<(), SessionError>

Available on crate feature advanced only.

Removes the session from the Database store.

THIS DOES NOT REMOVE THE KEY STORE.

ยงExamples
โ“˜
session.database_remove_session().await;

Trait Implementationsยง

Sourceยง

impl<T> Clone for Session<T>
where T: DatabasePool + Clone + Debug + Sync + Send + 'static + Clone,

Sourceยง

fn clone(&self) -> Session<T>

Returns a duplicate of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<T> Debug for Session<T>
where T: DatabasePool + Clone + Debug + Sync + Send + 'static + Debug,

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Sourceยง

impl<T> From<Session<T>> for ReadOnlySession<T>
where T: DatabasePool + Clone + Debug + Sync + Send + 'static,

Sourceยง

fn from(session: Session<T>) -> Self

Converts to this type from the input type.
Sourceยง

impl<T, S> FromRequestParts<S> for Session<T>
where T: DatabasePool + Clone + Debug + Sync + Send + 'static, S: Send + Sync,

Adds FromRequestParts<B> for Session

Returns the Session from Axumโ€™s request extensions state

Sourceยง

type Rejection = (StatusCode, &'static str)

If the extractor fails itโ€™ll use this โ€œrejectionโ€ type. A rejection is a kind of error that can be converted into a response.
Sourceยง

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.

Auto Trait Implementationsยง

ยง

impl<T> Freeze for Session<T>
where T: Freeze,

ยง

impl<T> !RefUnwindSafe for Session<T>

ยง

impl<T> Send for Session<T>

ยง

impl<T> Sync for Session<T>

ยง

impl<T> Unpin for Session<T>
where T: Unpin,

ยง

impl<T> UnsafeUnpin for Session<T>
where T: UnsafeUnpin,

ยง

impl<T> !UnwindSafe for Session<T>

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> FromRef<T> for T
where T: Clone,

Sourceยง

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Sourceยง

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Sourceยง

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails itโ€™ll use this โ€œrejectionโ€ type. A rejection is a kind of error that can be converted into a response.
Sourceยง

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Sourceยง

fn vzip(self) -> V

Sourceยง

impl<T> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,