-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to change password in the admin dashboard
- Loading branch information
1 parent
759a4cb
commit 331a553
Showing
14 changed files
with
232 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
use super::AppState; | ||
use axum::{routing::get, Router}; | ||
use axum::{ | ||
routing::{get, post}, | ||
Router, | ||
}; | ||
|
||
pub mod route; | ||
pub mod schema; | ||
|
||
pub fn router() -> Router<AppState> { | ||
Router::new().route("/app", get(route::admin_dashboard)) | ||
Router::new() | ||
.route("/app", get(route::admin_dashboard)) | ||
.route("/change-password", post(route::change_password)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,114 @@ | ||
use anyhow::Context; | ||
use askama::Template; | ||
use axum::{ | ||
body::Body, | ||
extract::State, | ||
http::{Response, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
Json, | ||
}; | ||
use secrecy::ExposeSecret; | ||
use sqlx::PgPool; | ||
use uuid::Uuid; | ||
|
||
use crate::app::extractor::session_user::SessionUser; | ||
use crate::app::{ | ||
authentication::{self, validate_credentials, Credentials}, | ||
extractor::session_user::SessionUser, | ||
AppState, | ||
}; | ||
|
||
use super::schema::ChangePasswordRequestBody; | ||
|
||
#[derive(Template)] | ||
#[template(path = "incorrect_username_or_password.html")] | ||
struct IncorrectPasswordTemplate; | ||
|
||
#[derive(Template)] | ||
#[template(path = "success.html")] | ||
struct Success { | ||
message: String, | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "admin_dashboard.html")] | ||
struct AdminDashboardTemplate { | ||
user: String, | ||
} | ||
|
||
#[tracing::instrument(name = "Admin dashboard", skip(session))] | ||
pub async fn admin_dashboard(session: Option<SessionUser>) -> impl IntoResponse { | ||
#[tracing::instrument(name = "Admin dashboard", skip(state, session))] | ||
pub async fn admin_dashboard( | ||
state: State<AppState>, | ||
session: Option<SessionUser>, | ||
) -> impl IntoResponse { | ||
if let Some(user) = session { | ||
Response::builder() | ||
.status(StatusCode::OK) | ||
.body(Body::from( | ||
AdminDashboardTemplate { user: user.id }.render().unwrap(), | ||
AdminDashboardTemplate { | ||
user: get_username(user.id, &state.db).await.unwrap(), | ||
} | ||
.render() | ||
.unwrap(), | ||
)) | ||
.unwrap() | ||
} else { | ||
Redirect::temporary("/login").into_response() | ||
} | ||
} | ||
|
||
#[tracing::instrument(name = "Change password", skip(user, state, body))] | ||
pub async fn change_password( | ||
user: SessionUser, | ||
state: State<AppState>, | ||
Json(body): Json<ChangePasswordRequestBody>, | ||
) -> impl IntoResponse { | ||
if body.new_password.expose_secret() != body.new_password_check.expose_secret() { | ||
return Response::builder() | ||
.status(StatusCode::OK) | ||
.body(Body::from(IncorrectPasswordTemplate.render().unwrap())) | ||
.unwrap(); | ||
} | ||
|
||
let username = get_username(user.id, &state.db).await.unwrap(); | ||
let credentials = Credentials { | ||
username, | ||
password: body.current_password, | ||
}; | ||
if validate_credentials(credentials, &state.db).await.is_err() { | ||
return Response::builder() | ||
.status(StatusCode::OK) | ||
.body(Body::from(IncorrectPasswordTemplate.render().unwrap())) | ||
.unwrap(); | ||
} | ||
|
||
authentication::change_password(user.id, body.new_password, &state.db) | ||
.await | ||
.unwrap(); | ||
|
||
Response::builder() | ||
.status(StatusCode::OK) | ||
.body(Body::from( | ||
Success { | ||
message: "Password successfully changed.".to_owned(), | ||
} | ||
.render() | ||
.unwrap(), | ||
)) | ||
.unwrap() | ||
} | ||
|
||
#[tracing::instrument(name = "Get username", skip(pool))] | ||
pub async fn get_username(user_id: Uuid, pool: &PgPool) -> Result<String, anyhow::Error> { | ||
let row = sqlx::query!( | ||
r#" | ||
select username | ||
from users | ||
where user_id = $1 | ||
"#, | ||
user_id, | ||
) | ||
.fetch_one(pool) | ||
.await | ||
.context("Failed to perform a query to retrieve a username.")?; | ||
Ok(row.username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use secrecy::Secret; | ||
|
||
#[derive(serde::Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct ChangePasswordRequestBody { | ||
pub current_password: Secret<String>, | ||
pub new_password: Secret<String>, | ||
pub new_password_check: Secret<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.