webs unified
This commit is contained in:
60
web/src/pages/login/logic.rs
Normal file
60
web/src/pages/login/logic.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use axum::{
|
||||
Form,
|
||||
extract::State,
|
||||
http::{HeaderMap, HeaderValue, StatusCode, header},
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use tonic::Request;
|
||||
|
||||
use crate::{AppState, auth::LoginRequest, ui::Nav};
|
||||
|
||||
use super::{state::LoginInput, ui};
|
||||
|
||||
pub(crate) async fn login_page(headers: HeaderMap) -> Html<String> {
|
||||
Html(ui::render_page(Nav::new(&headers, "login")))
|
||||
}
|
||||
|
||||
pub(crate) async fn login(
|
||||
State(state): State<AppState>,
|
||||
Form(input): Form<LoginInput>,
|
||||
) -> Response {
|
||||
if input.identifier.trim().is_empty() {
|
||||
return error(StatusCode::BAD_REQUEST, "Username or email is required");
|
||||
}
|
||||
|
||||
let mut client = state.auth;
|
||||
let login = match client
|
||||
.login(Request::new(LoginRequest {
|
||||
identifier: input.identifier,
|
||||
password: input.password,
|
||||
}))
|
||||
.await
|
||||
{
|
||||
Ok(response) => response.into_inner(),
|
||||
Err(status) => return error(StatusCode::UNAUTHORIZED, status.message()),
|
||||
};
|
||||
|
||||
let cookie = format!(
|
||||
"{}={}; Path=/; HttpOnly; SameSite=Strict; Max-Age={}",
|
||||
crate::ui::SESSION_COOKIE,
|
||||
login.access_token,
|
||||
login.expires_in,
|
||||
);
|
||||
let Ok(cookie) = HeaderValue::try_from(cookie) else {
|
||||
return error(
|
||||
StatusCode::BAD_GATEWAY,
|
||||
"The server returned an invalid access token",
|
||||
);
|
||||
};
|
||||
|
||||
let mut response = Html(String::new()).into_response();
|
||||
response.headers_mut().insert(header::SET_COOKIE, cookie);
|
||||
response
|
||||
.headers_mut()
|
||||
.insert("hx-redirect", HeaderValue::from_static("/admin"));
|
||||
response
|
||||
}
|
||||
|
||||
fn error(status: StatusCode, message: &str) -> Response {
|
||||
(status, Html(ui::render_error(message))).into_response()
|
||||
}
|
||||
Reference in New Issue
Block a user