redirect to /login and redirect back, instead of showing small msg
This commit is contained in:
@@ -71,7 +71,11 @@ pub async fn page(Query(q): Query<LoginMsg>, req: Request) -> Result<Response, A
|
||||
// (if javascript is disabled, login via form still works)
|
||||
script defer {(PreEscaped(r#"
|
||||
if (window.location.search) {
|
||||
history.replaceState(null, '', window.location.pathname);
|
||||
const url = new URL(window.location.href);
|
||||
const r = url.searchParams.get('r');
|
||||
url.search = '';
|
||||
if (r) url.searchParams.set('r', r);
|
||||
history.replaceState(null, '', url.pathname + url.search);
|
||||
}
|
||||
document.getElementById('login-form').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
@@ -89,7 +93,12 @@ pub async fn page(Query(q): Query<LoginMsg>, req: Request) -> Result<Response, A
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const r = new URL(window.location.href).searchParams.get('r');
|
||||
if (r && r.startsWith('/')) {
|
||||
window.location.href = r;
|
||||
} else {
|
||||
window.location.href = '/dashboard';
|
||||
}
|
||||
} else {
|
||||
const text = await res.text();
|
||||
err.textContent = text || 'Login failed';
|
||||
|
||||
@@ -9,12 +9,14 @@ use crate::{
|
||||
error::CompositeError,
|
||||
logs::LogEntry,
|
||||
users::{User, auth::UserAuthenticate, permissions::Permission},
|
||||
web::{RedirectViaError, components::nav::nav, icons, pages::base},
|
||||
web::{components::nav::nav, icons, pages::base},
|
||||
};
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(req.headers())?
|
||||
.ok_or(RedirectViaError(Redirect::to("/login?re=/logs")))?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction()?;
|
||||
let logs = LogEntry::get_all(&tx)?;
|
||||
@@ -63,7 +65,7 @@ pub async fn page(req: Request) -> Result<Response, CompositeError> {
|
||||
}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must have permission to view this page."}
|
||||
p class="text-center p-2" {"You must have permission to view logs."}
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
@@ -20,16 +20,18 @@ use crate::{
|
||||
};
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
Ok(base(
|
||||
"Persons | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
@if let Some(_) = u {
|
||||
div class="mx-auto max-w-4xl px-2 my-4" {
|
||||
p class="flex items-center gap-2" {
|
||||
span class="text-neutral-500" {(PreEscaped(icons::CONTACT))}
|
||||
@@ -82,9 +84,6 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
} @else {
|
||||
p class="text-red-400 text-center" {"Failed to load persons."}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must be logged in to view this page."}
|
||||
}
|
||||
),
|
||||
)
|
||||
.into_response())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::{
|
||||
extract::{Query, Request},
|
||||
response::{IntoResponse, Response},
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
use maud::{PreEscaped, html};
|
||||
use serde::Deserialize;
|
||||
@@ -9,10 +9,7 @@ use crate::{
|
||||
database,
|
||||
error::CompositeError,
|
||||
quotes::Quote,
|
||||
users::{
|
||||
User,
|
||||
auth::{UserAuthRequired, UserAuthenticate},
|
||||
},
|
||||
users::{User, auth::UserAuthenticate},
|
||||
web::{
|
||||
components::{nav::nav, quote::quote},
|
||||
icons,
|
||||
@@ -31,7 +28,10 @@ pub async fn page(
|
||||
Query(query): Query<PageQuery>,
|
||||
req: Request,
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(req.headers())?.required()?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let conn = database::conn()?;
|
||||
|
||||
let page = query.page.unwrap_or(1).max(1);
|
||||
|
||||
@@ -27,14 +27,17 @@ const LINE_ADD_RM_SCRIPT: &str = include_str!("line-add-rm.js");
|
||||
const PREFILL_TIME_SCRIPT: &str = include_str!("prefill-time.js");
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let conn = database::conn()?;
|
||||
let names = Name::get_all(&conn)?;
|
||||
|
||||
Ok(base(
|
||||
"Add Quote | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
div class="max-w-4xl mx-auto px-2" {
|
||||
div class="my-4 flex justify-between" {
|
||||
|
||||
@@ -20,15 +20,17 @@ use crate::{
|
||||
};
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let conn = database::conn()?;
|
||||
|
||||
Ok(base(
|
||||
"Tags | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
@if let Some(_) = u {
|
||||
div class="mx-auto max-w-4xl px-2 my-4" {
|
||||
p class="flex items-center gap-2" {
|
||||
span class="text-neutral-500" {(PreEscaped(icons::TAG))}
|
||||
@@ -81,9 +83,6 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
} @else {
|
||||
p class="text-red-400 text-center" {"Failed to load tags."}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must be logged in to view this page."}
|
||||
}
|
||||
),
|
||||
)
|
||||
.into_response())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::{
|
||||
extract::Request,
|
||||
response::{IntoResponse, Response},
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
use maud::{PreEscaped, html};
|
||||
|
||||
@@ -22,19 +22,18 @@ pub mod create;
|
||||
pub mod profile;
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let conn = database::conn()?;
|
||||
let us = match u.is_some() {
|
||||
true => User::get_all(&conn),
|
||||
false => Ok(vec![]),
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let conn = database::conn()?;
|
||||
let us = User::get_all(&conn);
|
||||
|
||||
Ok(base(
|
||||
"Users | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
@if let Some(u) = u {
|
||||
div class="mx-auto max-w-4xl px-2 my-4" {
|
||||
p class="flex items-center gap-2" {
|
||||
span class="text-neutral-500" {(PreEscaped(icons::USERS))}
|
||||
@@ -63,9 +62,6 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
p class="text-center py-4 text-light text-red-500" {"Failed to load users."}
|
||||
}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must be logged in to view this page."}
|
||||
}
|
||||
),
|
||||
)
|
||||
.into_response())
|
||||
|
||||
@@ -21,15 +21,17 @@ use crate::{
|
||||
};
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let conn = database::conn()?;
|
||||
|
||||
Ok(base(
|
||||
"Create User | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
@if let Some(u) = u {
|
||||
div class="mx-auto max-w-4xl px-2 my-4" {
|
||||
p class="flex items-center gap-2" {
|
||||
span class="text-neutral-500" {(PreEscaped(icons::USER_PLUS))}
|
||||
@@ -55,9 +57,6 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must have permission to view this page."}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must be logged in to view this page."}
|
||||
}
|
||||
),
|
||||
)
|
||||
.into_response())
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::{
|
||||
pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, CompositeError> {
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to("/users").into_response()),
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
@@ -20,14 +20,16 @@ use crate::{
|
||||
};
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
let u = User::authenticate(req.headers())?;
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
Some(u) => u,
|
||||
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
|
||||
};
|
||||
|
||||
Ok(base(
|
||||
"User Settings | Mnemosyne",
|
||||
html!(
|
||||
(nav(u.as_ref(), req.uri().path()))
|
||||
(nav(Some(&u), req.uri().path()))
|
||||
|
||||
@if let Some(u) = u {
|
||||
div class="max-w-4xl mx-auto px-2" {
|
||||
div class="mx-auto max-w-4xl my-4" {
|
||||
p class="flex items-center gap-2" {
|
||||
@@ -65,9 +67,6 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
|
||||
}
|
||||
}
|
||||
}
|
||||
} @else {
|
||||
p class="text-center p-2" {"You must be logged in to view this page."}
|
||||
}
|
||||
),
|
||||
)
|
||||
.into_response())
|
||||
|
||||
Reference in New Issue
Block a user