forked from Roflin/gamenight
Fixes the ugly Register User post handler.
This commit is contained in:
parent
3509a70a6a
commit
70ae15f655
53
backend-actix/Cargo.lock
generated
53
backend-actix/Cargo.lock
generated
@ -462,9 +462,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cxx"
|
name = "cxx"
|
||||||
version = "1.0.93"
|
version = "1.0.94"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a9c00419335c41018365ddf7e4d5f1c12ee3659ddcf3e01974650ba1de73d038"
|
checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"cxxbridge-flags",
|
"cxxbridge-flags",
|
||||||
@ -474,9 +474,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cxx-build"
|
name = "cxx-build"
|
||||||
version = "1.0.93"
|
version = "1.0.94"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fb8307ad413a98fff033c8545ecf133e3257747b3bae935e7602aab8aa92d4ca"
|
checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"codespan-reporting",
|
"codespan-reporting",
|
||||||
@ -484,24 +484,24 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"scratch",
|
"scratch",
|
||||||
"syn 2.0.10",
|
"syn 2.0.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cxxbridge-flags"
|
name = "cxxbridge-flags"
|
||||||
version = "1.0.93"
|
version = "1.0.94"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "edc52e2eb08915cb12596d29d55f0b5384f00d697a646dbd269b6ecb0fbd9d31"
|
checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cxxbridge-macro"
|
name = "cxxbridge-macro"
|
||||||
version = "1.0.93"
|
version = "1.0.94"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "631569015d0d8d54e6c241733f944042623ab6df7bc3be7466874b05fcdb1c5f"
|
checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.10",
|
"syn 2.0.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -634,9 +634,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "generic-array"
|
name = "generic-array"
|
||||||
version = "0.14.6"
|
version = "0.14.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
|
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"typenum",
|
"typenum",
|
||||||
"version_check",
|
"version_check",
|
||||||
@ -1078,9 +1078,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.53"
|
version = "1.0.54"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73"
|
checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
@ -1220,29 +1220,29 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.158"
|
version = "1.0.159"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9"
|
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.158"
|
version = "1.0.159"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad"
|
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.10",
|
"syn 2.0.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.94"
|
version = "1.0.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea"
|
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"ryu",
|
"ryu",
|
||||||
@ -1343,9 +1343,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.10"
|
version = "2.0.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40"
|
checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -1378,7 +1378,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.10",
|
"syn 2.0.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1436,14 +1436,13 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.26.0"
|
version = "1.27.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64"
|
checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"memchr",
|
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
pub mod request;
|
pub mod request;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
use actix_web::HttpServer;
|
use actix_web::HttpServer;
|
||||||
use actix_web::App;
|
use actix_web::App;
|
||||||
@ -13,7 +14,6 @@ pub(crate) type DbPool = Pool<ConnectionManager<PgConnection>>;
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
let url = "postgres://root:root@localhost/gamenight";
|
let url = "postgres://root:root@localhost/gamenight";
|
||||||
let manager = ConnectionManager::<PgConnection>::new(url);
|
let manager = ConnectionManager::<PgConnection>::new(url);
|
||||||
// Refer to the `r2d2` documentation for more methods to use
|
// Refer to the `r2d2` documentation for more methods to use
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post};
|
use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post};
|
||||||
use chrono::{DateTime, ParseError};
|
use chrono::{DateTime, ParseError};
|
||||||
|
|
||||||
use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::{GamenightPost, GamenightGet}, util::GetPgConnection}, schema::{self, user::User}};
|
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::schema::{self};
|
||||||
|
use crate::schema::user::User;
|
||||||
|
use crate::request::requests::GamenightGet;
|
||||||
|
use crate::request::requests::GamenightPost;
|
||||||
|
use crate::request::responses::GameNightResponse;
|
||||||
|
use crate::request::error::ApiError;
|
||||||
|
use crate::DbPool;
|
||||||
|
use crate::util::GetConnection;
|
||||||
|
|
||||||
impl GamenightPost {
|
impl GamenightPost {
|
||||||
pub fn into_with_user(&self, user: User) -> Result<schema::gamenight::Gamenight, ParseError> {
|
pub fn into_with_user(&self, user: User) -> Result<schema::gamenight::Gamenight, ParseError> {
|
||||||
return Ok(schema::gamenight::Gamenight {
|
return Ok(schema::gamenight::Gamenight {
|
||||||
|
@ -5,7 +5,6 @@ mod user_handlers;
|
|||||||
mod gamenight_handlers;
|
mod gamenight_handlers;
|
||||||
mod error;
|
mod error;
|
||||||
mod authorization;
|
mod authorization;
|
||||||
mod util;
|
|
||||||
|
|
||||||
pub use user_handlers::login;
|
pub use user_handlers::login;
|
||||||
pub use user_handlers::register;
|
pub use user_handlers::register;
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
use diesel::PgConnection;
|
||||||
|
use diesel::r2d2::ConnectionManager;
|
||||||
|
use diesel::r2d2::Pool;
|
||||||
|
|
||||||
use crate::schema::user::{unique_email, unique_username};
|
use crate::schema::user::{unique_email, unique_username};
|
||||||
|
|
||||||
@ -13,12 +16,12 @@ pub struct Login {
|
|||||||
pub struct Register {
|
pub struct Register {
|
||||||
#[validate(
|
#[validate(
|
||||||
length(min = 1),
|
length(min = 1),
|
||||||
custom(function = "unique_username", arg = "&'v_a mut diesel::PgConnection")
|
custom(function = "unique_username", arg = "&'v_a Pool<ConnectionManager<PgConnection>>")
|
||||||
)]
|
)]
|
||||||
pub username: String,
|
pub username: String,
|
||||||
#[validate(
|
#[validate(
|
||||||
email,
|
email,
|
||||||
custom(function = "unique_email", arg = "&'v_a mut diesel::PgConnection")
|
custom(function = "unique_email", arg = "&'v_a Pool<ConnectionManager<PgConnection>>")
|
||||||
)]
|
)]
|
||||||
#[validate(email)]
|
#[validate(email)]
|
||||||
pub email: String,
|
pub email: String,
|
||||||
|
@ -7,7 +7,7 @@ use crate::request::requests::{Login, Register};
|
|||||||
use crate::request::error::ApiError;
|
use crate::request::error::ApiError;
|
||||||
use crate::request::responses::LoginResponse;
|
use crate::request::responses::LoginResponse;
|
||||||
use crate::request::authorization::get_token;
|
use crate::request::authorization::get_token;
|
||||||
use crate::request::util::GetPgConnection;
|
use crate::util::GetConnection;
|
||||||
use crate::schema::{self};
|
use crate::schema::{self};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
@ -30,8 +30,6 @@ impl Into<schema::user::Register> for Register {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[get("/token")]
|
#[get("/token")]
|
||||||
pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Result<impl Responder, ApiError> {
|
pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Result<impl Responder, ApiError> {
|
||||||
let data = login_data.into_inner();
|
let data = login_data.into_inner();
|
||||||
@ -57,19 +55,12 @@ pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Res
|
|||||||
|
|
||||||
#[post("/user")]
|
#[post("/user")]
|
||||||
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register>) -> Result<impl Responder, ApiError> {
|
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register>) -> Result<impl Responder, ApiError> {
|
||||||
let data1 = register_data.clone();
|
web::block(move || -> Result<(), ApiError> {
|
||||||
let data2 = register_data.clone();
|
register_data.validate_args((&pool, &pool))?;
|
||||||
let register_request = data2.into();
|
let register_request = register_data.into_inner().into();
|
||||||
let mut conn1 = pool.get_conn();
|
let mut conn = pool.get_conn();
|
||||||
let mut conn2 = pool.get_conn();
|
schema::register(&mut conn, register_request)?;
|
||||||
|
Ok(())
|
||||||
let _validation_result = web::block(move || {
|
|
||||||
data1.validate_args((&mut conn1, &mut conn2))
|
|
||||||
}).await??;
|
|
||||||
|
|
||||||
let mut conn3 = pool.get_conn();
|
|
||||||
let _register_result = web::block(move || {
|
|
||||||
schema::register(&mut conn3, register_request)
|
|
||||||
}).await??;
|
}).await??;
|
||||||
|
|
||||||
return Ok(HttpResponse::Ok())
|
return Ok(HttpResponse::Ok())
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
use actix_web::web;
|
|
||||||
use diesel::{r2d2::{PooledConnection, ConnectionManager}, PgConnection};
|
|
||||||
|
|
||||||
use crate::DbPool;
|
|
||||||
|
|
||||||
pub trait GetPgConnection {
|
|
||||||
fn get_conn(&self) -> PooledConnection<ConnectionManager<PgConnection>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GetPgConnection for web::Data<DbPool> {
|
|
||||||
fn get_conn(&self) -> PooledConnection<ConnectionManager<PgConnection>> {
|
|
||||||
self.get().expect("couldn't get db connection from pool")
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,6 +11,9 @@ use argon2::{
|
|||||||
Argon2,
|
Argon2,
|
||||||
};
|
};
|
||||||
use validator::ValidationError;
|
use validator::ValidationError;
|
||||||
|
use crate::DbPool;
|
||||||
|
use crate::util::GetConnection;
|
||||||
|
|
||||||
use super::schema::{pwd, users};
|
use super::schema::{pwd, users};
|
||||||
|
|
||||||
pub use super::error::DatabaseError;
|
pub use super::error::DatabaseError;
|
||||||
@ -89,11 +92,13 @@ pub fn get_user(conn: &mut PgConnection, id: Uuid) -> Result<User, DatabaseError
|
|||||||
Ok(users::table.find(id).first(conn)?)
|
Ok(users::table.find(id).first(conn)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unique_username(username: &String, conn: &mut PgConnection) -> Result<(), ValidationError> {
|
pub fn unique_username(username: &String, pool: &DbPool) -> Result<(), ValidationError> {
|
||||||
|
let mut conn = pool.get().expect("Couldn't get db connection from pool");
|
||||||
|
|
||||||
match users::table
|
match users::table
|
||||||
.count()
|
.count()
|
||||||
.filter(users::username.eq(username))
|
.filter(users::username.eq(username))
|
||||||
.get_result(conn)
|
.get_result(&mut conn)
|
||||||
{
|
{
|
||||||
Ok(0) => Ok(()),
|
Ok(0) => Ok(()),
|
||||||
Ok(_) => Err(ValidationError::new("User already exists")),
|
Ok(_) => Err(ValidationError::new("User already exists")),
|
||||||
@ -101,11 +106,13 @@ pub fn unique_username(username: &String, conn: &mut PgConnection) -> Result<(),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unique_email(email: &String, conn: &mut PgConnection) -> Result<(), ValidationError> {
|
pub fn unique_email(email: &String, pool: &DbPool) -> Result<(), ValidationError> {
|
||||||
|
let mut conn = pool.get_conn();
|
||||||
|
|
||||||
match users::table
|
match users::table
|
||||||
.count()
|
.count()
|
||||||
.filter(users::email.eq(email))
|
.filter(users::email.eq(email))
|
||||||
.get_result(conn)
|
.get_result(&mut conn)
|
||||||
{
|
{
|
||||||
Ok(0) => Ok(()),
|
Ok(0) => Ok(()),
|
||||||
Ok(_) => Err(ValidationError::new("email already exists")),
|
Ok(_) => Err(ValidationError::new("email already exists")),
|
||||||
|
11
backend-actix/src/util/mod.rs
Normal file
11
backend-actix/src/util/mod.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use diesel::{r2d2::{PooledConnection, ManageConnection, Pool}};
|
||||||
|
|
||||||
|
pub trait GetConnection<T> where T: ManageConnection {
|
||||||
|
fn get_conn(&self) -> PooledConnection<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ManageConnection> GetConnection<T> for Pool<T> {
|
||||||
|
fn get_conn(&self) -> PooledConnection<T> {
|
||||||
|
self.get().expect("Couldn't get db connection from pool")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user