Crate actix_identity[][src]

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 {
    id.remember("User1".to_owned()); // <- remember identity
    HttpResponse::Ok().finish()
}

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

// create cookie identity backend
let policy = CookieIdentityPolicy::new(&[0; 32])
    .name("auth-cookie")
    .secure(false);

let app = App::new()
    // wrap policy into middleware identity middleware
    .wrap(IdentityService::new(policy))
    .service(services![index, login, 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.

RequestIdentity

Helper trait that allows to get Identity.