register user fix
This commit is contained in:
BIN
db/1.sqlite
BIN
db/1.sqlite
Binary file not shown.
BIN
db/1.sqlite-shm
BIN
db/1.sqlite-shm
Binary file not shown.
BIN
db/1.sqlite-wal
BIN
db/1.sqlite-wal
Binary file not shown.
Binary file not shown.
@@ -162,20 +162,20 @@ where S: Send + Sync,
|
|||||||
pub async fn register_user (
|
pub async fn register_user (
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
RegisterPayload(payload): RegisterPayload
|
RegisterPayload(payload): RegisterPayload
|
||||||
) -> Result<impl IntoResponse, (StatusCode, &'static str)> {
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||||
|
|
||||||
let hashed_password = hash_password(&payload.password)
|
let hashed_password = hash_password(&payload.password)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Password hashing failed"))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Password hashing failed: {}", e)))?;
|
||||||
|
|
||||||
let conn = state.logs_pool.get()
|
let conn = state.logs_pool.get()
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error"))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("DB connection error: {}", e)))?;
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO users (username, password, displayname)
|
"INSERT INTO users (username, password, displayname)
|
||||||
VALUES (?1, ?2, ?3)",
|
VALUES (?1, ?2, ?3)",
|
||||||
params![payload.username, hashed_password, payload.displayname],
|
params![payload.username, hashed_password, payload.displayname],
|
||||||
)
|
)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("User insert error: {}", e)))?;
|
||||||
|
|
||||||
let user_id = conn.last_insert_rowid();
|
let user_id = conn.last_insert_rowid();
|
||||||
|
|
||||||
@@ -186,23 +186,21 @@ pub async fn register_user (
|
|||||||
|
|
||||||
let hotel_name: String = conn
|
let hotel_name: String = conn
|
||||||
.query_row(
|
.query_row(
|
||||||
"SELECT hotel_name FROM hotels
|
"SELECT hotelname FROM hotels
|
||||||
WHERE id = ?1 ",
|
WHERE id = ?1 ",
|
||||||
params![hotel_id],
|
params![hotel_id],
|
||||||
|row| row.get(0),
|
|row| row.get(0),
|
||||||
).map_err(|_| (StatusCode::BAD_REQUEST, "Invalid hotel ids"))?;
|
).map_err(|e| (StatusCode::BAD_REQUEST, format!("Invalid hotel id {}: {}", hotel_id, e)))?;
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO hotel_user_link (user_id, hotel_id, username, hotel_name)
|
"INSERT INTO hotel_user_link (user_id, hotel_id, username, hotelname)
|
||||||
VALUES (?1, ?2, ?3, ?4)",
|
VALUES (?1, ?2, ?3, ?4)",
|
||||||
params![user_id, hotel_id, payload.username, hotel_name],
|
params![user_id, hotel_id, payload.username, hotel_name],
|
||||||
)
|
)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Link insert error"))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Link insert error for user_id={} hotel_id={}: {}", user_id, hotel_id, e)))?;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok((StatusCode::CREATED, "User registered successfully".to_string()))
|
||||||
Ok((StatusCode::CREATED, "User registered successfully"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -534,35 +532,31 @@ pub async fn create_refresh_token(
|
|||||||
|
|
||||||
/*.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Error mapping hotel_ids".to_string())); */
|
/*.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Error mapping hotel_ids".to_string())); */
|
||||||
|
|
||||||
let mut exist_stmt = conn.prepare(
|
|
||||||
"SELECT id FROM refresh_token
|
|
||||||
WHERE device_id = ?1 AND user_agent = ?2 AND user_id=?3"
|
|
||||||
) .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
||||||
|
|
||||||
let existing_token_id: i32 = match exist_stmt.query_one(
|
|
||||||
params![device_id_str,user_agent_str, user_id],
|
|
||||||
|row| row.get (0)
|
|
||||||
) {
|
|
||||||
Ok(id) => id,
|
|
||||||
Err(_) => return Err((StatusCode::INTERNAL_SERVER_ERROR, "error fetching credentials".to_string())),
|
|
||||||
};
|
|
||||||
|
|
||||||
match existing_token_id {
|
|
||||||
//placeholder functions and match arms
|
|
||||||
Some(id) => updateToken(id),
|
|
||||||
None(_) => createNewToken(),
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
//FIXME: might not need the hotel list on tconflict ?
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO refresh_token (user_id, token_hash, device_id, user_agent, hotel_id_list)
|
r#"
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
INSERT INTO refresh_token (
|
||||||
|
user_id,
|
||||||
|
token_hash,
|
||||||
|
device_id,
|
||||||
|
user_agent,
|
||||||
|
hotel_id_list
|
||||||
|
)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||||
|
ON CONFLICT(user_id, device_id, user_agent)
|
||||||
|
DO UPDATE SET
|
||||||
|
token_hash = excluded.token_hash,
|
||||||
|
hotel_id_list = excluded.hotel_id_list
|
||||||
|
"#,
|
||||||
params![
|
params![
|
||||||
&user_id,
|
user_id,
|
||||||
&hashed_token,
|
hashed_token,
|
||||||
&device_id_str,
|
device_id_str,
|
||||||
&user_agent_str,
|
user_agent_str,
|
||||||
&hotel_ids_json,
|
hotel_ids_json
|
||||||
],
|
],
|
||||||
).map_err(|e| {
|
).map_err(|e| {
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {}", e))
|
(StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {}", e))
|
||||||
|
|||||||
Reference in New Issue
Block a user