Crate actix_identity
source · [−]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 {
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()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
// create cookie identity backend
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])
})
.bind(("0.0.0.0", 8080u16))?
.run()
.await
}
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.