better authentication (why was this a macro)
This commit is contained in:
parent
790a3f4776
commit
6e672d3d0e
4 changed files with 48 additions and 42 deletions
|
@ -41,4 +41,5 @@ i've run out of ideas.
|
|||
- use log instead of println
|
||||
- make a proper rank system (reuploading, uploading music, rating, etc.)
|
||||
- user icons in the account management + settings (gdicon.oat.zone? selfhost?)
|
||||
- account settings page
|
||||
- account settings page
|
||||
- better web design
|
|
@ -1,19 +1,18 @@
|
|||
macro_rules! auth {
|
||||
($cookies: expr) => {
|
||||
match $cookies.get_private("blackmail_data") {
|
||||
Some(cookie_val) => {
|
||||
let parts = cookie_val.value().split(":").collect::<Vec<&str>>();
|
||||
use rocket::http::CookieJar;
|
||||
|
||||
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 fn authenticate(cookies: &CookieJar<'_>) -> Result<(String, i32, i32), &'static str> {
|
||||
match cookies.get_private("blackmail_data") {
|
||||
Some(cookie) => {
|
||||
let parts = cookie.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!");
|
||||
|
||||
return Ok((username, account_id, user_id))
|
||||
}
|
||||
None => {
|
||||
return Err("authentication failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) use auth;
|
||||
}
|
|
@ -12,26 +12,29 @@ use crate::db;
|
|||
pub fn account_management(cookies: &CookieJar<'_>) -> Result<Template, Redirect> {
|
||||
let connection = &mut db::establish_connection_pg();
|
||||
|
||||
let (logged_in, username_val, _account_id_val, user_id_val) = crate::helpers::templates::auth!(cookies);
|
||||
let logged_in = crate::helpers::templates::authenticate(cookies);
|
||||
|
||||
if logged_in {
|
||||
use crate::schema::users::dsl::*;
|
||||
use crate::models::User;
|
||||
|
||||
let result = users
|
||||
.filter(id.eq(user_id_val.expect("user_id not found")))
|
||||
.get_result::<User, >(connection)
|
||||
.expect("couldnt find user with user id from account");
|
||||
|
||||
return Ok(Template::render("account_management", context! {
|
||||
username: username_val.expect("username not found"),
|
||||
stars: result.stars,
|
||||
diamonds: result.diamonds,
|
||||
coins: result.coins,
|
||||
user_coins: result.user_coins,
|
||||
demons: result.demons
|
||||
}));
|
||||
} else {
|
||||
return Err(Redirect::to("/login"));
|
||||
match logged_in {
|
||||
Ok((username_val, account_id_val, user_id_val)) => {
|
||||
use crate::schema::users::dsl::*;
|
||||
use crate::models::User;
|
||||
|
||||
let result = users
|
||||
.filter(id.eq(user_id_val))
|
||||
.get_result::<User, >(connection)
|
||||
.expect("couldnt find user with user id from account");
|
||||
|
||||
return Ok(Template::render("account_management", context! {
|
||||
username: username_val,
|
||||
stars: result.stars,
|
||||
diamonds: result.diamonds,
|
||||
coins: result.coins,
|
||||
user_coins: result.user_coins,
|
||||
demons: result.demons
|
||||
}));
|
||||
},
|
||||
Err(_) => {
|
||||
return Err(Redirect::to("/login"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -65,11 +65,14 @@ pub fn post_login(cookies: &CookieJar<'_>, input: Form<FormLogin>) -> Template {
|
|||
|
||||
#[get("/login")]
|
||||
pub fn get_login(cookies: &CookieJar<'_>) -> Result<Redirect, Template> {
|
||||
let (logged_in, _username, _account_id, _user_id) = crate::helpers::templates::auth!(cookies);
|
||||
let logged_in = crate::helpers::templates::authenticate(cookies);
|
||||
|
||||
if logged_in {
|
||||
Ok(Redirect::to("/"))
|
||||
} else {
|
||||
Err(Template::render("login", context! { }))
|
||||
match logged_in {
|
||||
Ok(_) => {
|
||||
return Ok(Redirect::to("/"))
|
||||
},
|
||||
Err(_) => {
|
||||
Err(Template::render("login", context! { }))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue