some type of authentication for the site
This commit is contained in:
parent
b3451a641e
commit
cdaf5febb7
13 changed files with 318 additions and 7 deletions
|
@ -45,7 +45,7 @@ pub fn login_account(input: Form<FromLoginAccount>) -> status::Custom<&'static s
|
|||
let query_result = accounts
|
||||
.select(id)
|
||||
.filter(username.eq(input.userName.clone()))
|
||||
.get_result::<i32>(connection);
|
||||
.get_result::<i32, >(connection);
|
||||
|
||||
match query_result {
|
||||
Ok(account_id_val) => {
|
||||
|
|
|
@ -4,4 +4,5 @@ pub mod difficulty;
|
|||
pub mod encryption;
|
||||
pub mod format;
|
||||
pub mod levels;
|
||||
pub mod reupload;
|
||||
pub mod reupload;
|
||||
pub mod templates;
|
19
src/helpers/templates.rs
Normal file
19
src/helpers/templates.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
macro_rules! auth {
|
||||
($cookies: expr) => {
|
||||
match $cookies.get_private("blackmail_data") {
|
||||
Some(cookie_val) => {
|
||||
let parts = cookie_val.value().split(":").collect::<Vec<&str>>();
|
||||
|
||||
let username = parts[0].to_string();
|
||||
let account_id = parts[1].parse::<i32>().expect("account id is not an integer! this should NOT happen!");
|
||||
let user_id = parts[2].parse::<i32>().expect("user id is not an integer! this should NOT happen!");
|
||||
|
||||
(true, Some(username), Some(account_id), Some(user_id))
|
||||
}
|
||||
None => {
|
||||
(false, None, None, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) use auth;
|
|
@ -53,7 +53,12 @@ fn rocket() -> _ {
|
|||
template_endpoints::index::index,
|
||||
|
||||
template_endpoints::reupload::post_reupload,
|
||||
template_endpoints::reupload::get_reupload
|
||||
template_endpoints::reupload::get_reupload,
|
||||
|
||||
template_endpoints::login::post_login,
|
||||
template_endpoints::login::get_login,
|
||||
|
||||
template_endpoints::logout::logout,
|
||||
])
|
||||
// assets
|
||||
.mount("/", routes![
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
pub mod index;
|
||||
pub mod login;
|
||||
pub mod logout;
|
||||
pub mod reupload;
|
68
src/template_endpoints/login.rs
Normal file
68
src/template_endpoints/login.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use rocket::response::Redirect;
|
||||
|
||||
use rocket_dyn_templates::{Template, context};
|
||||
|
||||
use rocket::form::Form;
|
||||
|
||||
use rocket::http::{Cookie, CookieJar};
|
||||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::db;
|
||||
use crate::helpers;
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct FormLogin {
|
||||
username: String,
|
||||
password: String
|
||||
}
|
||||
|
||||
#[post("/login", data = "<input>")]
|
||||
pub fn post_login(jar: &CookieJar<'_>, input: Form<FormLogin>) -> Template {
|
||||
let connection = &mut db::establish_connection_pg();
|
||||
|
||||
use crate::schema::accounts::dsl::*;
|
||||
|
||||
let result = accounts
|
||||
.select((id, username))
|
||||
.filter(username.eq(input.username.clone()))
|
||||
.get_result::<(i32, String), >(connection);
|
||||
|
||||
match result {
|
||||
Ok(account_id_username_val) => {
|
||||
match helpers::accounts::auth(account_id_username_val.0, Some(input.password.clone()), None, None) {
|
||||
Ok(account_id_user_id_val) => {
|
||||
jar.add_private(Cookie::build(
|
||||
"blackmail_data",
|
||||
format!("{}:{}:{}", account_id_username_val.1, account_id_user_id_val.0, account_id_user_id_val.1))
|
||||
.finish());
|
||||
|
||||
return Template::render("login", context! {
|
||||
success: "Successfully logged in"
|
||||
})
|
||||
},
|
||||
Err(_) => {
|
||||
return Template::render("login", context! {
|
||||
error: "Invalid password"
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
return Template::render("login", context! {
|
||||
error: "Invalid username or password"
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/login")]
|
||||
pub fn get_login(cookies: &CookieJar<'_>) -> Result<Redirect, Template> {
|
||||
let (logged_in, _username, _account_id, _user_id) = crate::helpers::templates::auth!(cookies);
|
||||
|
||||
if logged_in {
|
||||
Ok(Redirect::to("/"))
|
||||
} else {
|
||||
Err(Template::render("login", context! { }))
|
||||
}
|
||||
}
|
11
src/template_endpoints/logout.rs
Normal file
11
src/template_endpoints/logout.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use rocket::http::{Cookie, CookieJar};
|
||||
use rocket::response::Redirect;
|
||||
|
||||
#[post("/accounts/logout")]
|
||||
pub fn logout(jar: &CookieJar<'_>) -> Redirect {
|
||||
jar.remove_private(Cookie::named("username"));
|
||||
jar.remove_private(Cookie::named("account_id"));
|
||||
jar.remove_private(Cookie::named("user_id"));
|
||||
|
||||
Redirect::to("/")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue