mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 15:51:06 +01:00
Include cookie expiration and ttl in the identity example (#473)
This commit is contained in:
parent
370f9d3033
commit
21680e0ebe
@ -13,10 +13,10 @@
|
|||||||
//! http -v --session=identity GET localhost:8080/
|
//! http -v --session=identity GET localhost:8080/
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use std::io;
|
use std::{io, time::Duration};
|
||||||
|
|
||||||
use actix_identity::{Identity, IdentityMiddleware};
|
use actix_identity::{Identity, IdentityMiddleware};
|
||||||
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
cookie::Key, get, middleware::Logger, post, App, HttpMessage, HttpRequest, HttpResponse,
|
cookie::Key, get, middleware::Logger, post, App, HttpMessage, HttpRequest, HttpResponse,
|
||||||
HttpServer, Responder,
|
HttpServer, Responder,
|
||||||
@ -28,16 +28,25 @@ async fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
let secret_key = Key::generate();
|
let secret_key = Key::generate();
|
||||||
|
|
||||||
|
let expiration = Duration::from_secs(24 * 60 * 60);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let session_mw =
|
let session_mw =
|
||||||
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
||||||
// disable secure cookie for local testing
|
// disable secure cookie for local testing
|
||||||
.cookie_secure(false)
|
.cookie_secure(false)
|
||||||
|
// Set a ttl for the cookie if the identity should live longer than the user session
|
||||||
|
.session_lifecycle(
|
||||||
|
PersistentSession::default().session_ttl(expiration.try_into().unwrap()),
|
||||||
|
)
|
||||||
.build();
|
.build();
|
||||||
|
let identity_mw = IdentityMiddleware::builder()
|
||||||
|
.visit_deadline(Some(expiration))
|
||||||
|
.build();
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
// Install the identity framework first.
|
// Install the identity framework first.
|
||||||
.wrap(IdentityMiddleware::default())
|
.wrap(identity_mw)
|
||||||
// The identity system is built on top of sessions. You must install the session
|
// The identity system is built on top of sessions. You must install the session
|
||||||
// middleware to leverage `actix-identity`. The session middleware must be mounted
|
// middleware to leverage `actix-identity`. The session middleware must be mounted
|
||||||
// AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
|
// AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
|
||||||
|
Loading…
Reference in New Issue
Block a user