1
0
mirror of https://github.com/actix/examples synced 2024-12-01 01:24:35 +01:00
examples/auth/cookie-auth/src/main.rs
2022-02-18 02:01:48 +00:00

53 lines
1.6 KiB
Rust

use actix_identity::Identity;
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use rand::Rng;
async fn index(id: Identity) -> String {
format!(
"Hello {}",
id.identity().unwrap_or_else(|| "Anonymous".to_owned())
)
}
async fn login(id: Identity) -> HttpResponse {
id.remember("user1".to_owned());
HttpResponse::Found()
.insert_header(("location", "/"))
.finish()
}
async fn logout(id: Identity) -> HttpResponse {
id.forget();
HttpResponse::Found()
.insert_header(("location", "/"))
.finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// Generate a random 32 byte key. Note that it is important to use a unique
// private key for every project. Anyone with access to the key can generate
// authentication cookies for any user!
let private_key = rand::thread_rng().gen::<[u8; 32]>();
HttpServer::new(move || {
App::new()
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&private_key)
.name("auth-example")
.secure(false),
))
// enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default())
.service(web::resource("/login").route(web::post().to(login)))
.service(web::resource("/logout").to(logout))
.service(web::resource("/").route(web::get().to(index)))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}