[−][src]Module actix_web::middleware::identity
Request identity service for Actix applications.
IdentityService middleware can be used with different policies types to store identity information.
By default, only cookie identity policy is implemented. Other backend implementations can be added separately.
CookieIdentityPolicy uses cookies as identity storage.
To access current request identity Identity extractor should be used.
use actix_web::middleware::identity::Identity; use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService}; use actix_web::*; fn index(id: Identity) -> String { // access request identity if let Some(id) = id.identity() { format!("Welcome! {}", id) } else { "Welcome Anonymous!".to_owned() } } fn login(id: Identity) -> HttpResponse { id.remember("User1".to_owned()); // <- remember identity HttpResponse::Ok().finish() } fn logout(id: Identity) -> HttpResponse { id.forget(); // <- remove identity HttpResponse::Ok().finish() } fn main() { let app = App::new().wrap(IdentityService::new( // <- create identity middleware CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend .name("auth-cookie") .secure(false))) .service(web::resource("/index.html").to(index)) .service(web::resource("/login.html").to(login)) .service(web::resource("/logout.html").to(logout)); }
Structs
CookieIdentityPolicy | Use cookies for request identity storage. |
Identity | The extractor type to obtain your identity from a request. |
IdentityService | Request identity middleware |
Traits
IdentityPolicy | Identity policy definition. |