Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Lesson 9): update identity and session management #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions slides/09-actix.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,40 +895,56 @@ async fn main() -> std::io::Result<()> {

---

# Autentizace
# Autentizace a session management

```rust
use actix_web::*;
use actix_identity::{Identity, CookieIdentityPolicy, IdentityService};
use actix_identity::IdentityMiddleware;
use actix_session::{storage::RedisSessionStore, SessionMiddleware};
use actix_web::{cookie::Key, App, HttpResponse, HttpServer};

async fn index(id: Identity) -> String {
// access request identity
if let Some(id) = id.identity() {
format!("Welcome! {}", id)
#[actix_web::main]
async fn main() {
let secret_key = Key::generate();
let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379")
.await
.unwrap();

HttpServer::new(move || {
App::new()
.wrap(IdentityMiddleware::default())
.wrap(SessionMiddleware::new(
redis_store.clone(),
secret_key.clone(),
))
})
}
```

---

# Autentizace a session management

```rust
#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
if let Some(user) = user {
format!("Welcome! {}", user.id().unwrap())
} else {
"Welcome Anonymous!".to_owned()
}
}

async fn login(id: Identity) -> HttpResponse {
id.remember("User1".to_owned()); // <- remember identity
HttpResponse::Ok().finish()
}

async fn logout(id: Identity) -> HttpResponse {
id.forget(); // <- remove identity
HttpResponse::Ok().finish()
#[post("/login")]
async fn login(request: HttpRequest) -> impl Responder {
// Verify user
Identity::login(&request.extensions(), "User1".into()).unwrap();
HttpResponse::Ok()
}

fn main() {
let app = App::new().wrap(IdentityService::new(
// <- create identity middleware
CookieIdentityPolicy::new(&[0; 32]) // <- create cookie identity policy
.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));
#[post("/logout")]
async fn logout(user: Identity) -> impl Responder {
user.logout();
HttpResponse::Ok()
}
```

Expand Down
Loading