Skip to main content

Guard

Struct Guard 

Source
pub struct Guard { /* private fields */ }
Expand description

Logging in and out, on top of one session.

Implementations§

Source§

impl Guard

Source

pub fn new(session: SessionHandle) -> Self

Source

pub fn login(&self, user: &impl Authenticatable) -> String

Log a user in, returning the session’s new id.

The session id is rotated first. See crate::Session::regenerate: a session id that was known before the login must not still be valid after it, or an attacker who planted the cookie inherits the login.

Source

pub fn login_using_id(&self, id: impl Into<String>) -> String

Log in by identifier, for a “log in as” or “remember me” flow that already knows the key without loading the user.

Source

pub fn logout(&self) -> String

Log out: empty the session and rotate its id.

Everything goes, not just the identifier. A cart, a half-filled form, a flashed message — all of it belonged to the person who just left, and on a shared machine the next person must not find any of it.

Source

pub fn user_id(&self) -> Option<String>

The authenticated user’s identifier, if there is one.

Source

pub fn check(&self) -> bool

Source

pub fn guest(&self) -> bool

Source

pub fn remember_intended(&self, path: impl Into<String>)

Remember where the visitor was heading before being sent to log in.

Stored rather than flashed: the visitor sees the login form, submits it, and only then is the destination needed — two requests later, by which time flash data would already be gone.

Source

pub fn intended(&self, fallback: &str) -> String

Take back that destination, or fallback if there is none.

Consumed on read, so a second login does not bounce the visitor to a page they asked for hours ago.

Source

pub fn session(&self) -> &SessionHandle

Auto Trait Implementations§

§

impl Freeze for Guard

§

impl RefUnwindSafe for Guard

§

impl Send for Guard

§

impl Sync for Guard

§

impl Unpin for Guard

§

impl UnsafeUnpin for Guard

§

impl UnwindSafe for Guard

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.