1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-04-23 02:14:52 +02:00
actix-extras/actix-identity/src/identity_ext.rs
Luca Palmieri b1cea64795
Rebuild actix-identity on top of actix-session (#246)
Co-authored-by: Rob Ede <robjtede@icloud.com>
2022-07-09 19:00:15 +01:00

28 lines
834 B
Rust

use actix_web::{dev::ServiceRequest, guard::GuardContext, HttpMessage, HttpRequest};
use crate::Identity;
/// Helper trait to retrieve an [`Identity`] instance from various `actix-web`'s types.
pub trait IdentityExt {
/// Retrieve the identity attached to the current session, if available.
fn get_identity(&self) -> Result<Identity, anyhow::Error>;
}
impl IdentityExt for HttpRequest {
fn get_identity(&self) -> Result<Identity, anyhow::Error> {
Identity::extract(&self.extensions())
}
}
impl IdentityExt for ServiceRequest {
fn get_identity(&self) -> Result<Identity, anyhow::Error> {
Identity::extract(&self.extensions())
}
}
impl<'a> IdentityExt for GuardContext<'a> {
fn get_identity(&self) -> Result<Identity, anyhow::Error> {
Identity::extract(&self.req_data())
}
}