mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-18 21:52:30 -04:00
feat(base): Add a From login response implementation for the Session
This commit is contained in:
@@ -44,3 +44,13 @@ pub struct Session {
|
||||
/// The ID of the client device
|
||||
pub device_id: DeviceIdBox,
|
||||
}
|
||||
|
||||
impl From<ruma::api::client::r0::session::login::Response> for Session {
|
||||
fn from(response: ruma::api::client::r0::session::login::Response) -> Self {
|
||||
Self {
|
||||
access_token: response.access_token,
|
||||
user_id: response.user_id,
|
||||
device_id: response.device_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1213,6 +1213,28 @@ impl Client {
|
||||
/// # anyhow::Result::<()>::Ok(()) });
|
||||
/// ```
|
||||
///
|
||||
/// The `Session` object can also be created from the response the
|
||||
/// [`Client::login()`] method returns:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use matrix_sdk::{Client, Session, ruma::{DeviceIdBox, user_id}};
|
||||
/// # use url::Url;
|
||||
/// # use futures::executor::block_on;
|
||||
/// # block_on(async {
|
||||
///
|
||||
/// let homeserver = Url::parse("http://example.com")?;
|
||||
/// let client = Client::new(homeserver)?;
|
||||
///
|
||||
/// let session: Session = client
|
||||
/// .login("example", "my-password", None, None)
|
||||
/// .await?
|
||||
/// .into();
|
||||
///
|
||||
/// // Persist the `Session` so it can later be used to restore the login.
|
||||
/// // client.restore_session(session).await?;
|
||||
/// # anyhow::Result::<()>::Ok(()) });
|
||||
/// ```
|
||||
///
|
||||
/// [`login`]: #method.login
|
||||
pub async fn restore_login(&self, session: Session) -> Result<()> {
|
||||
Ok(self.base_client.restore_login(session).await?)
|
||||
|
||||
Reference in New Issue
Block a user