Expand description

Opinionated request identity service for Actix Web apps.

IdentityService middleware can be used with different policies types to store identity information.

A cookie based policy is provided. CookieIdentityPolicy uses cookies as identity storage.

To access current request identity, use the Identity extractor.

use actix_web::*;
use actix_identity::{Identity, CookieIdentityPolicy, IdentityService};

#[get("/")]
async fn index(id: Identity) -> String {
    // access request identity
    if let Some(id) = id.identity() {
        format!("Welcome! {}", id)
    } else {
        "Welcome Anonymous!".to_owned()
    }
}

#[post("/login")]
async fn login(id: Identity) -> HttpResponse {
    // remember identity
    id.remember("User1".to_owned());
    HttpResponse::Ok().finish()
}

#[post("/logout")]
async fn logout(id: Identity) -> HttpResponse {
    // remove identity
    id.forget();
    HttpResponse::Ok().finish()
}

HttpServer::new(move || {
    // create cookie identity backend (inside closure, since policy is not Clone)
    let policy = CookieIdentityPolicy::new(&[0; 32])
        .name("auth-cookie")
        .secure(false);

    App::new()
        // wrap policy into middleware identity middleware
        .wrap(IdentityService::new(policy))
        .service(services![index, login, logout])
})

Structs

Use cookies for request identity storage.

The extractor type to obtain your identity from a request.

Request identity middleware

Traits

Identity policy.

Helper trait that allows to get Identity.