web UI, tailwind, icons, login
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use axum::{
|
||||
Json,
|
||||
Form, Json,
|
||||
http::{HeaderMap, header},
|
||||
response::{IntoResponse, Response},
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
@@ -20,10 +20,9 @@ pub struct LoginForm {
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub async fn login(Json(creds): Json<LoginForm>) -> Result<Response, AuthError> {
|
||||
fn login_common(creds: LoginForm) -> Result<(String, String), AuthError> {
|
||||
let u = authenticate_via_credentials(&creds.handle, &creds.password)?.required()?;
|
||||
let (_, token) = Session::new_for_user(&u)?;
|
||||
|
||||
let secure = match cfg!(debug_assertions) {
|
||||
false => "; Secure",
|
||||
true => "",
|
||||
@@ -33,9 +32,20 @@ pub async fn login(Json(creds): Json<LoginForm>) -> Result<Response, AuthError>
|
||||
Session::DEFAULT_PROLONGATION.num_seconds(),
|
||||
secure
|
||||
);
|
||||
|
||||
Ok((token, cookie))
|
||||
}
|
||||
pub async fn login(Json(creds): Json<LoginForm>) -> Result<Response, AuthError> {
|
||||
let (token, cookie) = login_common(creds)?;
|
||||
Ok(([(header::SET_COOKIE, cookie)], token).into_response())
|
||||
}
|
||||
pub async fn login_form(Form(creds): Form<LoginForm>) -> Result<Response, AuthError> {
|
||||
match login_common(creds) {
|
||||
Ok((_, cookie)) => {
|
||||
Ok(([(header::SET_COOKIE, cookie)], Redirect::to("/dashboard")).into_response())
|
||||
}
|
||||
Err(e) => Ok(Redirect::to(&format!("/login?msg={}", e.to_string())).into_response()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn logout(headers: HeaderMap) -> Result<Response, AuthError> {
|
||||
let mut s = Session::authenticate(&headers)?.required()?;
|
||||
|
||||
@@ -24,6 +24,7 @@ pub fn api_router() -> Router {
|
||||
.route("/api/live", get(async || "Mnemosyne lives"))
|
||||
// auth
|
||||
.route("/api/auth/login", post(auth::login))
|
||||
.route("/api/auth/login-form", post(auth::login_form))
|
||||
.route("/api/auth/logout", post(auth::logout))
|
||||
// users
|
||||
.route("/api/users", get(users::get_all))
|
||||
|
||||
Reference in New Issue
Block a user