Flash messages using axum-messages
This commit is contained in:
94
src/routes/login.rs
Normal file
94
src/routes/login.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use crate::{
|
||||
authentication::{AuthError, Credentials, validate_credentials},
|
||||
routes::error_chain_fmt,
|
||||
startup::AppState,
|
||||
};
|
||||
use axum::{
|
||||
Form, Json,
|
||||
extract::State,
|
||||
response::{Html, IntoResponse, Redirect, Response},
|
||||
};
|
||||
use axum_messages::Messages;
|
||||
use reqwest::StatusCode;
|
||||
use secrecy::SecretString;
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(thiserror::Error)]
|
||||
pub enum LoginError {
|
||||
#[error("Something went wrong.")]
|
||||
UnexpectedError(#[from] anyhow::Error),
|
||||
#[error("Authentication failed.")]
|
||||
AuthError(#[source] anyhow::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for LoginError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
error_chain_fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for LoginError {
|
||||
fn into_response(self) -> Response {
|
||||
#[derive(serde::Serialize)]
|
||||
struct ErrorResponse<'a> {
|
||||
message: &'a str,
|
||||
}
|
||||
|
||||
tracing::error!("{:?}", self);
|
||||
|
||||
match &self {
|
||||
LoginError::UnexpectedError(_) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(ErrorResponse {
|
||||
message: "An internal server error occured.",
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
LoginError::AuthError(_) => Redirect::to("/login").into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct LoginFormData {
|
||||
username: String,
|
||||
password: SecretString,
|
||||
}
|
||||
|
||||
pub async fn get_login(messages: Messages) -> impl IntoResponse {
|
||||
let mut error_html = String::new();
|
||||
for message in messages {
|
||||
writeln!(error_html, "<p><i>{}</i></p>", message).unwrap();
|
||||
}
|
||||
Html(format!(include_str!("login/login.html"), error_html))
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
skip(connection_pool, form),
|
||||
fields(username=tracing::field::Empty, user_id=tracing::field::Empty)
|
||||
)]
|
||||
pub async fn post_login(
|
||||
messages: Messages,
|
||||
State(AppState {
|
||||
connection_pool, ..
|
||||
}): State<AppState>,
|
||||
Form(form): Form<LoginFormData>,
|
||||
) -> Result<Redirect, LoginError> {
|
||||
let credentials = Credentials {
|
||||
username: form.username,
|
||||
password: form.password,
|
||||
};
|
||||
tracing::Span::current().record("username", tracing::field::display(&credentials.username));
|
||||
let user_id = validate_credentials(credentials, &connection_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
AuthError::UnexpectedError(_) => LoginError::UnexpectedError(e.into()),
|
||||
AuthError::InvalidCredentials(_) => {
|
||||
let e = LoginError::AuthError(e.into());
|
||||
messages.error(e.to_string());
|
||||
e
|
||||
}
|
||||
})?;
|
||||
tracing::Span::current().record("user_id", tracing::field::display(&user_id));
|
||||
Ok(Redirect::to("/"))
|
||||
}
|
||||
Reference in New Issue
Block a user