Split up files and add templating
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
08a6e5b0fa
commit
a1474dab43
8 changed files with 738 additions and 123 deletions
43
nixie-server/src/users/login.rs
Normal file
43
nixie-server/src/users/login.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use axum::Form;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::Html;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Redirect;
|
||||
|
||||
use crate::AuthSession;
|
||||
use crate::UserCredentials;
|
||||
|
||||
pub async fn show_login() -> Html<String> {
|
||||
format!(
|
||||
r##"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<form action="/login" method="POST">
|
||||
<input type="text" name="username"/>
|
||||
<input type="password" name="password"/>
|
||||
<input type="submit" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
"##
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
pub async fn do_login(
|
||||
mut auth_session: AuthSession,
|
||||
Form(creds): Form<UserCredentials>,
|
||||
) -> impl IntoResponse {
|
||||
let user = match auth_session.authenticate(creds.clone()).await {
|
||||
Ok(Some(user)) => user,
|
||||
Ok(None) => return StatusCode::UNAUTHORIZED.into_response(),
|
||||
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
};
|
||||
|
||||
if auth_session.login(&user).await.is_err() {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||
}
|
||||
|
||||
Redirect::to("/protected").into_response()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue