Use db_read_user() in message function

break-database
~erin 2021-07-23 13:10:42 -04:00
parent d5be76624c
commit e5da6dd774
Signed by: erin
GPG Key ID: DA70E064A8C70F44
1 changed files with 18 additions and 22 deletions

View File

@ -2,7 +2,7 @@
extern crate log; extern crate log;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::sync::Mutex; use std::sync::Mutex;
use crate::file_io::db_read; use crate::file_io::db_read_user;
use rocket::http::{Cookie, Cookies}; use rocket::http::{Cookie, Cookies};
use crate::message::{Message, MessageInput, MessageType}; use crate::message::{Message, MessageInput, MessageType};
use rocket_contrib::json::{Json, JsonValue}; use rocket_contrib::json::{Json, JsonValue};
@ -51,27 +51,23 @@ fn create_message(message: Json<MessageInput>, user: &User) -> JsonValue {
// Check if user can create the message, and then create more info about the message // Check if user can create the message, and then create more info about the message
fn check_token(token: Cookie, message: Json<MessageInput<'_>>) -> JsonValue { fn check_token(token: Cookie, message: Json<MessageInput<'_>>) -> JsonValue {
// check if token is correct for name given if let Some(user) = db_read_user(&message.name.to_lowercase()).ok().flatten() {
let users: Vec<User> = db_read(); // create vector out of users in json file // check if token is correct for name given
for i in &users { if token.value() == "NULL" {
// loop through elements warn!("NULL token!");
if i.name == message.name.to_lowercase() { // if it finds the user in the file return json!({
if token.value() == "NULL" { "status": "fail",
warn!("NULL token!"); "reason": "NULL token",
return json!({ });
"status": "fail", } else if user.session_token == token.value() { // if token matches
"reason": "NULL token", info!("user exists and given token matches");
}); return create_message(message, &user);
} else if i.session_token == token.value() { // if token matches } else {
info!("user exists and given token matches"); warn!("token does not match!");
return create_message(message, i); return json!({
} else { "status": "fail",
warn!("token does not match!"); "reason": "token does not match",
return json!({ })
"status": "fail",
"reason": "token does not match",
})
};
}; };
}; };
warn!("user not found"); warn!("user not found");