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 (
id SERIAL PRIMARY KEY,
username varchar(255) NOT NULL,
username varchar(255) UNIQUE NOT NULL,
password bytea
);

View File

@ -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<PgPool>,
Query(query_params): Query<Signup>,
) -> impl IntoResponse {
println!("{:?}", query_params);
async fn signup(State(pool): State<PgPool>, Json(signup): Json<Signup>) -> 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)