1
0
mirror of https://github.com/actix/examples synced 2025-03-01 20:24:18 +01:00

38 lines
1.2 KiB
Rust
Raw Normal View History

2019-03-09 21:08:08 -08:00
use actix_web::middleware::identity::Identity;
2018-07-21 14:33:02 +05:30
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
2019-03-09 21:08:08 -08:00
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
2018-04-18 16:46:35 -07:00
2019-03-09 21:08:08 -08:00
fn index(id: Identity) -> String {
format!("Hello {}", id.identity().unwrap_or("Anonymous".to_owned()))
2018-04-18 16:46:35 -07:00
}
2019-03-09 21:08:08 -08:00
fn login(id: Identity) -> HttpResponse {
id.remember("user1".to_owned());
2018-05-08 11:08:43 -07:00
HttpResponse::Found().header("location", "/").finish()
2018-04-18 16:46:35 -07:00
}
2019-03-09 21:08:08 -08:00
fn logout(id: Identity) -> HttpResponse {
id.forget();
2018-05-08 11:08:43 -07:00
HttpResponse::Found().header("location", "/").finish()
2018-04-18 16:46:35 -07:00
}
2019-03-09 21:08:08 -08:00
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
2018-04-18 16:46:35 -07:00
env_logger::init();
2019-03-09 21:08:08 -08:00
HttpServer::new(|| {
2018-05-08 11:08:43 -07:00
App::new()
2019-03-26 04:29:00 +01:00
.wrap(middleware::Logger::default())
.wrap(IdentityService::new(
2018-04-18 16:46:35 -07:00
CookieIdentityPolicy::new(&[0; 32])
.name("auth-example")
2018-05-08 11:08:43 -07:00
.secure(false),
))
2019-03-09 21:08:08 -08:00
.service(web::resource("/login").route(web::post().to(login)))
.service(web::resource("/logout").to(logout))
.service(web::resource("/").route(web::get().to(index)))
2019-03-09 18:03:09 -08:00
})
2019-03-09 21:08:08 -08:00
.bind("127.0.0.1:8080")?
.run()
2018-04-18 16:46:35 -07:00
}