Web applications sometimes need to show a one-time notification to the user - e.g. an error message after having failed to login.
These notifications are commonly called flash messages.
actix-web-flash-messages provides a framework to work with flash messages in actix-web, closely modeled after Django's message framework.
use ;
use ;
use Write;
/// Attach two flash messages to the outgoing response,
/// a redirect.
async
/// Pick up the flash messages attached to the request, showing
/// them to the user via the request body.
async
How to install
Add actix-web-flash-messages to your dependencies:
[]
# ...
= "4"
= "0.4"
By default, actix-web-flash-messages does not provide any storage backend to receive and send flash messages.
You can enable:
- a cookie-based one,
storage::CookieMessageStore, using thecookiesfeature flag. The cookie store uses a signed cookie to store and retrieve messages;
[]
# ...
= { = "0.4", = ["cookies"] }
- a session-based one, [
storage::SessionMessageStore], using thesessionsfeature flag. The session store attaches flash messages to the current session.
[]
# ...
= { = "0.4", = ["sessions"] }
You can provide a different message store by implementing the storage::FlashMessageStore trait.
Examples
You can find examples of application using actix-web-flash-messages on GitHub:
The Structure of a Flash Message
FlashMessages are made of a Level and a string of content.
The message level can be used for filtering and rendering - for example:
- Only show flash messages at
infolevel or above in a production environment, while retainingdebuglevel messages for local development; - Use different colours, in the UI, to display messages (e.g. red for errors, orange for warnings, etc.);
You can build a FlashMessage via FlashMessage::new by specifying its content and Level.
You can also use the shorter level-based constructors - e.g. FlashMessage::info.
Enabling Flash Messages
To start sending and receiving flash messages you need to register FlashMessagesFramework as a middleware on your actix_web's App:
use ;
use ;
use Key;
async
You will then be able to:
- extract
FlashMessages from incoming requests using theIncomingFlashMessagesextractor; - send
FlashMessages alongside the outgoing response usingFlashMessage::send.
use ;
use ;
/// Send a flash messages alongside the outgoing response, a redirect.
async
/// Extract the flash message from the incoming request.
async
Framework Configuration
There are a few knobs that you can tweak when it comes to FlashMessagesFramework.
Use FlashMessagesFramework::builder to get access to its fluent configuration API, built around FlashMessagesFrameworkBuilder.
Minimum Level
By default, FlashMessagesFramework will only dispatch messages at info-level or above, discarding debug-level messages.
You can change this setting using FlashMessagesFrameworkBuilder::minimum_level.
use ;
use ;
async
Message Storage
actix-web-flash-messages provides a cookie-based implementation of flash messages, storage::CookieMessageStore, using a signed cookie to store and retrieve messages.
You can provide a different message store by implementing the storage::FlashMessageStore trait.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.