mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
frontend
This commit is contained in:
45
crates/frontend/src/routes/login.rs
Normal file
45
crates/frontend/src/routes/login.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use actix_session::Session;
|
||||
use actix_web::{
|
||||
error::ErrorUnauthorized,
|
||||
web::{Data, Form, Redirect},
|
||||
HttpRequest, HttpResponse, Responder,
|
||||
};
|
||||
use askama::Template;
|
||||
use rustical_store::auth::AuthenticationProvider;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/login.html")]
|
||||
struct LoginPage;
|
||||
|
||||
pub async fn route_get_login() -> impl Responder {
|
||||
LoginPage
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PostLoginForm {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub async fn route_post_login<AP: AuthenticationProvider>(
|
||||
req: HttpRequest,
|
||||
form: Form<PostLoginForm>,
|
||||
session: Session,
|
||||
auth_provider: Data<AP>,
|
||||
) -> HttpResponse {
|
||||
// TODO: implement auth check
|
||||
dbg!(&form.username, &form.password);
|
||||
if let Ok(Some(user)) = auth_provider
|
||||
.validate_user_token(&form.username, &form.password)
|
||||
.await
|
||||
{
|
||||
session.insert("user", user).unwrap();
|
||||
Redirect::to(format!("/frontend/user/{}", &form.username))
|
||||
.see_other()
|
||||
.respond_to(&req)
|
||||
.map_into_boxed_body()
|
||||
} else {
|
||||
ErrorUnauthorized("Unauthorized").error_response()
|
||||
}
|
||||
}
|
||||
1
crates/frontend/src/routes/mod.rs
Normal file
1
crates/frontend/src/routes/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod login;
|
||||
Reference in New Issue
Block a user