2023-04-03 16:11:26 +02:00
|
|
|
use rocket::{
|
|
|
|
form::Form,
|
2023-04-03 17:32:41 +02:00
|
|
|
get,
|
|
|
|
http::{Cookie, CookieJar},
|
|
|
|
post,
|
2023-04-03 16:11:26 +02:00
|
|
|
request::FlashMessage,
|
2023-04-03 17:32:41 +02:00
|
|
|
response::{Flash, Redirect},
|
|
|
|
routes, FromForm, Route, State,
|
2023-04-03 16:11:26 +02:00
|
|
|
};
|
2023-04-04 10:44:14 +02:00
|
|
|
use rocket_dyn_templates::{context, tera, Template};
|
2023-04-03 17:32:41 +02:00
|
|
|
use serde_json::json;
|
2023-04-03 16:11:26 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
use crate::model::user::{LoginError, User};
|
2023-04-03 16:11:26 +02:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
async fn index(flash: Option<FlashMessage<'_>>) -> Template {
|
|
|
|
let mut context = tera::Context::new();
|
|
|
|
|
|
|
|
if let Some(msg) = flash {
|
|
|
|
context.insert("flash", &msg.into_inner());
|
|
|
|
}
|
|
|
|
|
|
|
|
Template::render("auth/login", context.into_json())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct LoginForm {
|
|
|
|
name: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/", data = "<login>")]
|
2023-04-03 17:32:41 +02:00
|
|
|
async fn login(
|
|
|
|
login: Form<LoginForm>,
|
|
|
|
db: &State<SqlitePool>,
|
|
|
|
cookies: &CookieJar<'_>,
|
|
|
|
) -> Flash<Redirect> {
|
2023-04-03 16:11:26 +02:00
|
|
|
let user = User::login(db, login.name.clone(), login.password.clone()).await;
|
|
|
|
|
2023-04-03 22:03:45 +02:00
|
|
|
//TODO: be able to use ? for login. This would get rid of the following match clause.
|
2023-04-03 16:11:26 +02:00
|
|
|
let user = match user {
|
|
|
|
Ok(user) => user,
|
2023-04-04 10:44:14 +02:00
|
|
|
Err(LoginError::NoPasswordSet(user)) => {
|
|
|
|
return Flash::warning(
|
|
|
|
Redirect::to(format!("/auth/set-pw/{}", user.id)),
|
|
|
|
"Setze ein neues Passwort",
|
|
|
|
);
|
|
|
|
}
|
2023-04-03 16:11:26 +02:00
|
|
|
Err(_) => {
|
|
|
|
return Flash::error(Redirect::to("/auth"), "Falscher Benutzername/Passwort");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-03 17:32:41 +02:00
|
|
|
let user_json: String = format!("{}", json!(user));
|
2023-04-03 22:03:45 +02:00
|
|
|
cookies.add_private(Cookie::new("loggedin_user", user_json));
|
2023-04-03 17:32:41 +02:00
|
|
|
|
2023-04-03 16:11:26 +02:00
|
|
|
Flash::success(Redirect::to("/"), "Login erfolgreich")
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
#[get("/set-pw/<userid>")]
|
|
|
|
async fn setpw(userid: i32) -> Template {
|
|
|
|
Template::render("auth/set-pw", context!(userid))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UpdatePw {
|
|
|
|
userid: i32,
|
|
|
|
password: String,
|
|
|
|
password_confirm: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/set-pw", data = "<updatepw>")]
|
|
|
|
async fn updatepw(
|
|
|
|
db: &State<SqlitePool>,
|
|
|
|
updatepw: Form<UpdatePw>,
|
|
|
|
cookies: &CookieJar<'_>,
|
|
|
|
) -> Flash<Redirect> {
|
|
|
|
let user = User::find_by_id(db, updatepw.userid).await;
|
|
|
|
let user = match user {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_) => {
|
|
|
|
return Flash::error(
|
|
|
|
Redirect::to("/auth"),
|
|
|
|
format!("User with ID {} does not exist!", updatepw.userid),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if updatepw.password != updatepw.password_confirm {
|
|
|
|
return Flash::error(
|
|
|
|
Redirect::to(format!("/auth/set-pw/{}", updatepw.userid)),
|
|
|
|
"Passwörter stimmen nicht überein! Bitte probiere es nochmal",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
user.update_pw(db, updatepw.password.clone()).await;
|
|
|
|
|
|
|
|
let user_json: String = format!("{}", json!(user));
|
|
|
|
cookies.add_private(Cookie::new("loggedin_user", user_json));
|
|
|
|
|
|
|
|
Flash::success(
|
|
|
|
Redirect::to("/"),
|
|
|
|
"Passwort erfolgreich gesetzt. Du bist nun eingeloggt.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-03 22:10:12 +02:00
|
|
|
#[get("/logout")]
|
|
|
|
async fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash<Redirect> {
|
|
|
|
cookies.remove_private(Cookie::named("loggedin_user"));
|
|
|
|
|
|
|
|
Flash::success(Redirect::to("/auth"), "Logout erfolgreich")
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:11:26 +02:00
|
|
|
pub fn routes() -> Vec<Route> {
|
2023-04-04 10:44:14 +02:00
|
|
|
routes![index, login, logout, setpw, updatepw]
|
2023-04-03 16:11:26 +02:00
|
|
|
}
|