From cc66a25d5db5d2570c944dd267a9f1f266ab5158 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Tue, 4 Apr 2023 13:59:27 -0500 Subject: [PATCH] signup function --- migrations/20230402213003_user.sql | 2 +- src/main.rs | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/migrations/20230402213003_user.sql b/migrations/20230402213003_user.sql index bb20d06..15c19dc 100644 --- a/migrations/20230402213003_user.sql +++ b/migrations/20230402213003_user.sql @@ -1,6 +1,6 @@ CREATE TABLE users ( id SERIAL PRIMARY KEY, - username varchar(255) NOT NULL, + username varchar(255) UNIQUE NOT NULL, password bytea ); diff --git a/src/main.rs b/src/main.rs index 3eac076..84b98eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ async fn main() { // build our application with a route let app = Router::new() // `GET /` goes to `root` - .route("/", post(root)) + .route("/", get(root)) // `POST /users` goes to `create_user` .route("/user/signup", post(signup)) .with_state(pool); @@ -45,16 +45,14 @@ async fn main() { // basic handler that responds with a static string async fn root() -> &'static str { + println!("jjjj",); "Hello, World!" } -async fn signup( - State(pool): State, - Query(query_params): Query, -) -> impl IntoResponse { - println!("{:?}", query_params); +async fn signup(State(pool): State, Json(signup): Json) -> impl IntoResponse { + println!("{:?}", signup); // insert your application logic here - let pass_hash = sha256::digest(&*query_params.password); + let pass_hash = sha256::digest(&*signup.password); let result = sqlx::query!( r#" @@ -62,7 +60,7 @@ INSERT INTO users (username, password) VALUES ( $1, $2 ) RETURNING id; "#, - query_params.username, + signup.username, pass_hash.as_bytes(), ) .fetch_one(&pool)