Request identity service for scrappy 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 scrappy::*;
use scrappy_identity::{Identity, CookieIdentityPolicy, IdentityService};
async fn index(id: Identity) -> String {
if let Some(id) = id.identity() {
format!("Welcome! {}", id)
} else {
"Welcome Anonymous!".to_owned()
}
}
async fn login(id: Identity) -> HttpResponse {
id.remember("User1".to_owned()); HttpResponse::Ok().finish()
}
async fn logout(id: Identity) -> HttpResponse {
id.forget(); HttpResponse::Ok().finish()
}
fn main() {
let app = App::new().wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32]) .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));
}