Added change password functionality

This commit is contained in:
xeovalyte 2024-10-02 12:31:59 +02:00
parent 4d52d73275
commit 062fbf6bbc
Signed by: xeovalyte
SSH Key Fingerprint: SHA256:kSQDrQDmKzljJzfGYcd3m9RqHi4h8rSwkZ3sQ9kBURo
2 changed files with 68 additions and 4 deletions

View File

@ -105,6 +105,16 @@ async fn change_email(new_email: String) -> Result<(), ServerFnError> {
fn Password() -> Element { fn Password() -> Element {
let mut is_open = use_signal(|| false); let mut is_open = use_signal(|| false);
let mut input_old_password = use_signal(|| "".to_string());
let mut input_new_password = use_signal(|| "".to_string());
let mut input_new_password_repeat = use_signal(|| "".to_string());
let submit = move |_| async move {
if let Ok(_) = change_password(input_old_password(), input_new_password()).await {
tracing::info!("User password changed");
};
};
rsx! { rsx! {
div { div {
class: "collapse collapse-arrow bg-base-200", class: "collapse collapse-arrow bg-base-200",
@ -117,7 +127,7 @@ fn Password() -> Element {
div { div {
class: "collapse-content", class: "collapse-content",
div { div {
class: "pl-2 space-y-3", class: "pl-2 flex flex-col gap-3",
label { label {
class: "form-control w-full", class: "form-control w-full",
div { div {
@ -125,8 +135,10 @@ fn Password() -> Element {
span { class: "label-text", "Huidig wachtwoord" } span { class: "label-text", "Huidig wachtwoord" }
} }
input { input {
r#type: "text", r#type: "password",
class: "input input-bordered w-full", class: "input input-bordered w-full",
oninput: move |event| input_old_password.set(event.value()),
value: "{input_old_password}",
} }
} }
label { label {
@ -136,8 +148,10 @@ fn Password() -> Element {
span { class: "label-text", "Nieuw wachtwoord" } span { class: "label-text", "Nieuw wachtwoord" }
} }
input { input {
r#type: "text", r#type: "password",
class: "input input-bordered w-full", class: "input input-bordered w-full",
oninput: move |event| input_new_password.set(event.value()),
value: "{input_new_password}",
} }
} }
label { label {
@ -147,8 +161,18 @@ fn Password() -> Element {
span { class: "label-text", "Herhaal nieuw wachtwoord" } span { class: "label-text", "Herhaal nieuw wachtwoord" }
} }
input { input {
r#type: "text", r#type: "password",
class: "input input-bordered w-full", class: "input input-bordered w-full",
oninput: move |event| input_new_password_repeat.set(event.value()),
value: "{input_new_password_repeat}",
}
}
div {
class: "w-full flex mt-5",
button {
class: "ml-auto btn btn-primary",
onclick: submit,
"Opslaan",
} }
} }
} }
@ -156,3 +180,12 @@ fn Password() -> Element {
}, },
} }
} }
#[server]
async fn change_password(old_password: String, new_password: String) -> Result<(), ServerFnError> {
let user = Session::fetch_current_user().await?;
user.change_password(old_password, new_password).await?;
Ok(())
}

View File

@ -75,4 +75,35 @@ impl User {
None => Err(crate::Error::NoDocument), None => Err(crate::Error::NoDocument),
} }
} }
pub async fn change_password(
&self,
old_password: String,
new_password: String,
) -> Result<(), crate::Error> {
let mut res = DB
.query(
"
LET $user = (SELECT * FROM ONLY type::thing('user', $user_id));
IF crypto::argon2::compare($user.password, $old_password) {
UPDATE type::thing('user', $user_id) SET password = crypto::argon2::generate($new_password)
} ELSE {
THROW 'Password dit not match'
};
",
)
.bind(("user_id", self.id.to_string()))
.bind(("old_password", old_password))
.bind(("new_password", new_password))
.await?;
tracing::info!("{res:?}");
if res.take_errors().len() != 0 {
return Err(crate::Error::NoDocument);
}
Ok(())
}
} }