Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.

Commit e9df06d

Browse files
feat(user-management): add reset password (#43)
1 parent 3968ddf commit e9df06d

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/user_management/operations.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod get_magic_auth;
1515
mod get_password_reset;
1616
mod get_user_identities;
1717
mod list_users;
18+
mod reset_password;
1819

1920
pub use authenticate_with_code::*;
2021
pub use authenticate_with_email_verification::*;
@@ -33,3 +34,4 @@ pub use get_magic_auth::*;
3334
pub use get_password_reset::*;
3435
pub use get_user_identities::*;
3536
pub use list_users::*;
37+
pub use reset_password::*;
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use async_trait::async_trait;
2+
use serde::Serialize;
3+
use thiserror::Error;
4+
5+
use crate::user_management::{PasswordResetToken, User, UserManagement};
6+
use crate::{ResponseExt, WorkOsError, WorkOsResult};
7+
8+
/// The parameters for [`ResetPassword`].
9+
#[derive(Debug, Serialize)]
10+
pub struct ResetPasswordParams<'a> {
11+
/// The `token` query parameter from the password reset URL.
12+
pub token: &'a PasswordResetToken,
13+
14+
/// The new password to set for the user.
15+
pub new_password: &'a str,
16+
}
17+
18+
/// An error returned from [`ResetPassword`].
19+
#[derive(Debug, Error)]
20+
pub enum ResetPasswordError {}
21+
22+
impl From<ResetPasswordError> for WorkOsError<ResetPasswordError> {
23+
fn from(err: ResetPasswordError) -> Self {
24+
Self::Operation(err)
25+
}
26+
}
27+
28+
/// [WorkOS Docs: Reset the password](https://workos.com/docs/reference/user-management/password-reset/reset-password)
29+
#[async_trait]
30+
pub trait ResetPassword {
31+
/// Sets a new password using the token query parameter from the link that the user received.
32+
///
33+
/// [WorkOS Docs: Reset the password](https://workos.com/docs/reference/user-management/password-reset/reset-password)
34+
///
35+
/// # Examples
36+
///
37+
/// ```
38+
/// use std::collections::HashSet;
39+
///
40+
/// # use workos_sdk::WorkOsResult;
41+
/// # use workos_sdk::user_management::*;
42+
/// use workos_sdk::{ApiKey, WorkOs};
43+
///
44+
/// # async fn run() -> WorkOsResult<(), ResetPasswordError> {
45+
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
46+
///
47+
/// let user = workos
48+
/// .user_management()
49+
/// .reset_password(&ResetPasswordParams {
50+
/// token: &PasswordResetToken::from("stpIJ48IFJt0HhSIqjf8eppe0"),
51+
/// new_password: "i8uv6g34kd490s",
52+
/// })
53+
/// .await?;
54+
/// # Ok(())
55+
/// # }
56+
/// ```
57+
async fn reset_password(
58+
&self,
59+
params: &ResetPasswordParams<'_>,
60+
) -> WorkOsResult<User, ResetPasswordError>;
61+
}
62+
63+
#[async_trait]
64+
impl ResetPassword for UserManagement<'_> {
65+
async fn reset_password(
66+
&self,
67+
params: &ResetPasswordParams<'_>,
68+
) -> WorkOsResult<User, ResetPasswordError> {
69+
let url = self
70+
.workos
71+
.base_url()
72+
.join("/user_management/password_reset/confirm")?;
73+
74+
let user = self
75+
.workos
76+
.client()
77+
.post(url)
78+
.bearer_auth(self.workos.key())
79+
.json(&params)
80+
.send()
81+
.await?
82+
.handle_unauthorized_or_generic_error()?
83+
.json::<User>()
84+
.await?;
85+
86+
Ok(user)
87+
}
88+
}
89+
90+
#[cfg(test)]
91+
mod test {
92+
use serde_json::json;
93+
use tokio;
94+
95+
use crate::user_management::UserId;
96+
use crate::{ApiKey, WorkOs};
97+
98+
use super::*;
99+
100+
#[tokio::test]
101+
async fn it_calls_the_reset_password_endpoint() {
102+
let mut server = mockito::Server::new_async().await;
103+
104+
let workos = WorkOs::builder(&ApiKey::from("sk_example_123456789"))
105+
.base_url(&server.url())
106+
.unwrap()
107+
.build();
108+
109+
server
110+
.mock("POST", "/user_management/password_reset/confirm")
111+
.match_header("Authorization", "Bearer sk_example_123456789")
112+
.with_status(201)
113+
.with_body(
114+
json!({
115+
"object": "user",
116+
"id": "user_01E4ZCR3C56J083X43JQXF3JK5",
117+
"email": "marcelina.davis@example.com",
118+
"first_name": "Marcelina",
119+
"last_name": "Davis",
120+
"email_verified": true,
121+
"profile_picture_url": "https://workoscdn.com/images/v1/123abc",
122+
"metadata": {},
123+
"created_at": "2021-06-25T19:07:33.155Z",
124+
"updated_at": "2021-06-25T19:07:33.155Z"
125+
})
126+
.to_string(),
127+
)
128+
.create_async()
129+
.await;
130+
131+
let user = workos
132+
.user_management()
133+
.reset_password(&ResetPasswordParams {
134+
token: &PasswordResetToken::from("stpIJ48IFJt0HhSIqjf8eppe0"),
135+
new_password: "i8uv6g34kd490s",
136+
})
137+
.await
138+
.unwrap();
139+
140+
assert_eq!(user.id, UserId::from("user_01E4ZCR3C56J083X43JQXF3JK5"))
141+
}
142+
}

0 commit comments

Comments
 (0)