signup function

This commit is contained in:
Mitchell Marino 2023-04-04 13:59:27 -05:00
parent 6c8d0e4bd4
commit cc66a25d5d
2 changed files with 7 additions and 9 deletions

View File

@ -1,6 +1,6 @@
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
username varchar(255) NOT NULL, username varchar(255) UNIQUE NOT NULL,
password bytea password bytea
); );

View File

@ -28,7 +28,7 @@ async fn main() {
// build our application with a route // build our application with a route
let app = Router::new() let app = Router::new()
// `GET /` goes to `root` // `GET /` goes to `root`
.route("/", post(root)) .route("/", get(root))
// `POST /users` goes to `create_user` // `POST /users` goes to `create_user`
.route("/user/signup", post(signup)) .route("/user/signup", post(signup))
.with_state(pool); .with_state(pool);
@ -45,16 +45,14 @@ async fn main() {
// basic handler that responds with a static string // basic handler that responds with a static string
async fn root() -> &'static str { async fn root() -> &'static str {
println!("jjjj",);
"Hello, World!" "Hello, World!"
} }
async fn signup( async fn signup(State(pool): State<PgPool>, Json(signup): Json<Signup>) -> impl IntoResponse {
State(pool): State<PgPool>, println!("{:?}", signup);
Query(query_params): Query<Signup>,
) -> impl IntoResponse {
println!("{:?}", query_params);
// insert your application logic here // insert your application logic here
let pass_hash = sha256::digest(&*query_params.password); let pass_hash = sha256::digest(&*signup.password);
let result = sqlx::query!( let result = sqlx::query!(
r#" r#"
@ -62,7 +60,7 @@ INSERT INTO users (username, password)
VALUES ( $1, $2 ) VALUES ( $1, $2 )
RETURNING id; RETURNING id;
"#, "#,
query_params.username, signup.username,
pass_hash.as_bytes(), pass_hash.as_bytes(),
) )
.fetch_one(&pool) .fetch_one(&pool)