1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00

Merge pull request #31 from sadika9/split-auth-example

Split auth example
This commit is contained in:
Nikolay Kim 2018-07-23 03:14:11 -07:00 committed by GitHub
commit cfb980642b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 10 deletions

View File

@ -38,6 +38,7 @@ script:
cd async_ex1 && cargo check && cd ..
cd basics && cargo check && cd ..
cd cookie-auth && cargo check && cd ..
cd cookie-auth-full && cargo check && cd ..
cd cookie-session && cargo check && cd ..
cd diesel && cargo check && cd ..
cd form && cargo check && cd ..

9
Cargo.lock generated
View File

@ -380,6 +380,15 @@ dependencies = [
[[package]]
name = "cookie-auth"
version = "0.1.0"
dependencies = [
"actix 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cookie-auth-full"
version = "0.1.0"
dependencies = [
"actix 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -5,6 +5,7 @@ members = [
"async_ex1",
"basics",
"cookie-auth",
"cookie-auth-full",
"cookie-session",
"diesel",
"form",

View File

@ -0,0 +1,14 @@
[package]
name = "cookie-auth-full"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = "../"
[dependencies]
actix = "0.7"
actix-web = "0.7"
cookie = { version="0.10", features=["percent-encode", "secure"] }
futures = "0.1"
time = "0.1"
env_logger = "0.5"

View File

@ -0,0 +1,49 @@
extern crate actix;
extern crate actix_web;
extern crate cookie;
extern crate env_logger;
extern crate futures;
extern crate time;
use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
mod auth;
use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity};
fn index(req: &HttpRequest) -> String {
format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned()))
}
fn login(req: &HttpRequest) -> HttpResponse {
req.remember("user1".to_owned());
HttpResponse::Found().header("location", "/").finish()
}
fn logout(req: &HttpRequest) -> HttpResponse {
req.forget();
HttpResponse::Found().header("location", "/").finish()
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let sys = actix::System::new("cookie-auth");
server::new(|| {
App::new()
.middleware(middleware::Logger::default())
.middleware(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32])
.name("auth-example")
.secure(false),
))
.resource("/login", |r| r.f(login))
.resource("/logout", |r| r.f(logout))
.resource("/", |r| r.f(index))
}).bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}

View File

@ -7,8 +7,4 @@ workspace = "../"
[dependencies]
actix = "0.7"
actix-web = "0.7"
cookie = { version="0.10", features=["percent-encode", "secure"] }
futures = "0.1"
time = "0.1"
env_logger = "0.5"

View File

@ -1,14 +1,10 @@
extern crate actix;
extern crate actix_web;
extern crate cookie;
extern crate env_logger;
extern crate futures;
extern crate time;
use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
mod auth;
use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity};
use actix_web::middleware::identity::RequestIdentity;
use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
fn index(req: &HttpRequest) -> String {
format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned()))