[][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 RequestIdentity should be used. HttpRequest implements RequestIdentity trait.

use actix_web::middleware::identity::RequestIdentity;
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
use actix_web::*;

fn index(req: HttpRequest) -> Result<String> {
    // access request identity
    if let Some(id) = req.identity() {
        Ok(format!("Welcome! {}", id))
    } else {
        Ok("Welcome Anonymous!".to_owned())
    }
}

fn login(mut req: HttpRequest) -> HttpResponse {
    req.remember("User1".to_owned()); // <- remember identity
    HttpResponse::Ok().finish()
}

fn logout(mut req: HttpRequest) -> HttpResponse {
    req.forget(); // <- remove identity
    HttpResponse::Ok().finish()
}

fn main() {
    let app = App::new().middleware(IdentityService::new(
        // <- create identity middleware
        CookieIdentityPolicy::new(&[0; 32])    // <- create cookie session backend
              .name("auth-cookie")
              .secure(false),
    ));
}

Structs

CookieIdentityPolicy

Use cookies for request identity storage.

IdentityService

Request identity middleware

Traits

Identity

An identity

IdentityPolicy

Identity policy definition.

RequestIdentity

The helper trait to obtain your identity from a request.