feat(api): add api for updating username, email, password, and deleting account
This commit is contained in:
parent
9f63255599
commit
aa6a48a09b
@ -14,7 +14,10 @@ pub fn get_routes() -> Router {
|
||||
Router::new()
|
||||
.route("/register", post(register))
|
||||
.route("/login", post(login))
|
||||
.route("/change_username", post(change_username)),
|
||||
.route("/change_username", post(change_username))
|
||||
.route("/change_email", post(change_email))
|
||||
.route("/change_password", post(change_password))
|
||||
.route("/delete_account", post(delete_account)),
|
||||
)
|
||||
}
|
||||
|
||||
@ -160,3 +163,76 @@ pub async fn change_username(Json(payload): Json<ChangeUsernamePayload>) -> Json
|
||||
connection.close().expect("Failed to close");
|
||||
Json(json!({ "success": "Username changed with success" }))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ChangeEmailPayload {
|
||||
email: String,
|
||||
token: String,
|
||||
}
|
||||
|
||||
pub async fn change_email(Json(payload): Json<ChangeEmailPayload>) -> Json<Value> {
|
||||
let account_id = match get_account_id_from_jwt(&payload.token) {
|
||||
Ok(account_id) => account_id,
|
||||
Err(_) => return Json(json!({ "error": "Invalid token" })),
|
||||
};
|
||||
|
||||
let connection = Connection::open("database.db").expect("Failed to open database");
|
||||
match db::change_email(&connection, account_id, &payload.email) {
|
||||
Ok(_) => {}
|
||||
Err(rusqlite::Error::SqliteFailure(_, _)) => {
|
||||
connection.close().expect("Failed to close");
|
||||
return Json(json!({ "error": "Email already taken" }));
|
||||
}
|
||||
Err(_) => {
|
||||
connection.close().expect("Failed to close");
|
||||
return Json(json!({ "error": "Failed to change email" }));
|
||||
}
|
||||
}
|
||||
connection.close().expect("Failed to close");
|
||||
Json(json!({ "success": "Email changed with success" }))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ChangePasswordPayload {
|
||||
password: String,
|
||||
token: String,
|
||||
}
|
||||
|
||||
pub async fn change_password(Json(payload): Json<ChangePasswordPayload>) -> Json<Value> {
|
||||
let account_id = match get_account_id_from_jwt(&payload.token) {
|
||||
Ok(account_id) => account_id,
|
||||
Err(_) => return Json(json!({ "error": "Invalid token" })),
|
||||
};
|
||||
let connection = Connection::open("database.db").expect("Failed to open database");
|
||||
match db::change_password(&connection, account_id, &payload.password) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
connection.close().expect("Failed to close");
|
||||
return Json(json!({ "error": "Failed to change password" }));
|
||||
}
|
||||
}
|
||||
connection.close().expect("Failed to close");
|
||||
Json(json!({ "success": "Password changed with success" }))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct DeleteAccountPayload {
|
||||
token: String,
|
||||
}
|
||||
|
||||
pub async fn delete_account(Json(payload): Json<DeleteAccountPayload>) -> Json<Value> {
|
||||
let account_id = match get_account_id_from_jwt(&payload.token) {
|
||||
Ok(account_id) => account_id,
|
||||
Err(_) => return Json(json!({ "error": "Invalid token" })),
|
||||
};
|
||||
let connection = Connection::open("database.db").expect("Failed to open database");
|
||||
match db::delete_account(&connection, account_id) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
connection.close().expect("Failed to close");
|
||||
return Json(json!({ "error": "Failed to delete account" }));
|
||||
}
|
||||
}
|
||||
connection.close().expect("Failed to close");
|
||||
Json(json!({ "success": "Account deleted with success" }))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user