forked from Roflin/gamenight
Fixes the review comments
This commit is contained in:
parent
81e65b1619
commit
5f73d556c6
@ -1,4 +1,4 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
drop table user;
|
||||
drop table pwd;
|
||||
drop table user;
|
@ -6,6 +6,7 @@ CREATE TABLE user (
|
||||
);
|
||||
|
||||
CREATE TABLE pwd (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
password TEXT NOT NULL
|
||||
user_id INTEGER NOT NULL PRIMARY KEY,
|
||||
password TEXT NOT NULL,
|
||||
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
|
||||
);
|
@ -8,23 +8,14 @@ use jsonwebtoken::DecodingKey;
|
||||
use jsonwebtoken::Validation;
|
||||
use jsonwebtoken::{EncodingKey, Header};
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::Outcome::{Failure, Success};
|
||||
use rocket::request::Outcome;
|
||||
use rocket::request::{self, FromRequest, Request};
|
||||
use rocket::request::{FromRequest, Request};
|
||||
use rocket::serde::json::{json, Json, Value};
|
||||
use rocket::State;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
use validator::ValidateArgs;
|
||||
|
||||
pub struct Referer(String);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ReferrerError {
|
||||
Missing,
|
||||
MoreThanOne,
|
||||
}
|
||||
|
||||
#[derive(Debug, Responder)]
|
||||
pub enum ApiResponseVariant {
|
||||
Status(Status),
|
||||
@ -33,20 +24,6 @@ pub enum ApiResponseVariant {
|
||||
// Flash(Flash<Redirect>)
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for Referer {
|
||||
type Error = ReferrerError;
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
||||
let referers: Vec<_> = req.headers().get("Referer").collect();
|
||||
match referers.len() {
|
||||
0 => Failure((Status::BadRequest, ReferrerError::Missing)),
|
||||
1 => Success(Referer(referers[0].to_string())),
|
||||
_ => Failure((Status::BadRequest, ReferrerError::MoreThanOne)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct ApiResponse {
|
||||
result: Cow<'static, str>,
|
||||
|
@ -45,8 +45,15 @@ fn rocket() -> _ {
|
||||
.attach(Template::fairing())
|
||||
.attach(AdHoc::on_ignite("Run Migrations", schema::run_migrations))
|
||||
.attach(AdHoc::config::<AppConfig>())
|
||||
.attach(site::make_cors())
|
||||
.mount("/", routes![site::index, site::files])
|
||||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
site::index,
|
||||
site::gamenights,
|
||||
site::add_game_night,
|
||||
site::register
|
||||
],
|
||||
)
|
||||
.mount(
|
||||
"/api",
|
||||
routes![
|
||||
|
@ -104,7 +104,7 @@ pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), Databas
|
||||
Err(error) => return Err(DatabaseError::Hash(error)),
|
||||
};
|
||||
|
||||
match conn
|
||||
let user_insert_result = conn
|
||||
.run(move |c| {
|
||||
c.transaction(|| {
|
||||
diesel::insert_into(user::table)
|
||||
@ -133,8 +133,9 @@ pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), Databas
|
||||
.execute(c)
|
||||
})
|
||||
})
|
||||
.await
|
||||
{
|
||||
.await;
|
||||
|
||||
match user_insert_result {
|
||||
Err(e) => Err(DatabaseError::Query(e.to_string())),
|
||||
_ => Ok(()),
|
||||
}
|
||||
|
@ -1,42 +1,90 @@
|
||||
use rocket::fs::NamedFile;
|
||||
use rocket::http::Method;
|
||||
use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions};
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use crate::schema;
|
||||
use rocket::request::FlashMessage;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_dyn_templates::Template;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub fn make_cors() -> Cors {
|
||||
let allowed_origins = AllowedOrigins::some_exact(&[
|
||||
// 4.
|
||||
//CHANGE THESE TO MATCH YOUR PORTS
|
||||
"http://localhost:3000",
|
||||
"http://127.0.0.1:3000",
|
||||
"http://localhost:8000",
|
||||
"http://0.0.0.0:8000",
|
||||
]);
|
||||
CorsOptions {
|
||||
// 5.
|
||||
allowed_origins,
|
||||
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(), // 1.
|
||||
allowed_headers: AllowedHeaders::some(&[
|
||||
"Authorization",
|
||||
"Accept",
|
||||
"Access-Control-Allow-Origin", // 6.
|
||||
]),
|
||||
allow_credentials: true,
|
||||
..Default::default()
|
||||
}
|
||||
.to_cors()
|
||||
.expect("error while building CORS")
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct FlashData {
|
||||
has_data: bool,
|
||||
kind: Cow<'static, str>,
|
||||
message: Cow<'static, str>,
|
||||
}
|
||||
|
||||
#[get("/<file..>")]
|
||||
pub async fn files(file: PathBuf) -> Option<NamedFile> {
|
||||
NamedFile::open(Path::new("../frontend/build/").join(file))
|
||||
.await
|
||||
.ok()
|
||||
impl FlashData {
|
||||
const EMPTY: Self = Self {
|
||||
has_data: false,
|
||||
message: Cow::Borrowed(""),
|
||||
kind: Cow::Borrowed(""),
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct GameNightsData {
|
||||
gamenights: Vec<schema::GameNight>,
|
||||
flash: FlashData,
|
||||
}
|
||||
|
||||
#[get("/gamenights")]
|
||||
pub async fn gamenights(conn: schema::DbConn) -> Template {
|
||||
let gamenights = schema::get_all_gamenights(conn).await;
|
||||
|
||||
let data = GameNightsData {
|
||||
gamenights: gamenights,
|
||||
flash: FlashData::EMPTY,
|
||||
};
|
||||
|
||||
Template::render("gamenights", &data)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub async fn index() -> io::Result<NamedFile> {
|
||||
NamedFile::open("../frontend/build/index.html").await
|
||||
pub async fn index() -> Redirect {
|
||||
Redirect::to(uri!(gamenights))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct GameNightAddData {
|
||||
post_url: String,
|
||||
flash: FlashData,
|
||||
}
|
||||
|
||||
#[get("/gamenight/add")]
|
||||
pub async fn add_game_night(flash: Option<FlashMessage<'_>>) -> Template {
|
||||
let flash_data = match flash {
|
||||
None => FlashData::EMPTY,
|
||||
Some(flash) => FlashData {
|
||||
has_data: true,
|
||||
message: Cow::Owned(flash.message().to_string()),
|
||||
kind: Cow::Owned(flash.kind().to_string()),
|
||||
},
|
||||
};
|
||||
|
||||
let data = GameNightAddData {
|
||||
post_url: "/api/gamenight".to_string(),
|
||||
flash: flash_data,
|
||||
};
|
||||
|
||||
Template::render("gamenight_add", &data)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct RegisterData {
|
||||
flash: FlashData,
|
||||
}
|
||||
|
||||
#[get("/register")]
|
||||
pub async fn register(flash: Option<FlashMessage<'_>>) -> Template {
|
||||
let flash_data = match flash {
|
||||
None => FlashData::EMPTY,
|
||||
Some(flash) => FlashData {
|
||||
has_data: true,
|
||||
message: Cow::Owned(flash.message().to_string()),
|
||||
kind: Cow::Owned(flash.kind().to_string()),
|
||||
},
|
||||
};
|
||||
|
||||
let data = RegisterData { flash: flash_data };
|
||||
|
||||
Template::render("register", &data)
|
||||
}
|
||||
|
@ -16,4 +16,4 @@
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>1
|
||||
</html>
|
@ -1,4 +1,4 @@
|
||||
ex<!DOCTYPE html>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
|
Loading…
Reference in New Issue
Block a user