Skip to content

Commit 53853c2

Browse files
committed
refactor(multiverse): Put the login logic into a separate function
1 parent 40de714 commit 53853c2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

labs/multiverse/src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::HashMap,
33
io::{self, stdout, Write},
4-
path::PathBuf,
4+
path::{Path, PathBuf},
55
sync::{Arc, Mutex},
66
time::Duration,
77
};
@@ -983,26 +983,34 @@ async fn configure_client(cli: Cli) -> Result<Client> {
983983
let client = client_builder.build().await?;
984984

985985
// Try reading a session, otherwise create a new one.
986-
let session_path = config_path.join("session.json");
986+
log_in_or_restore_session(&client, &session_path).await?;
987+
988+
Ok(client)
989+
}
990+
991+
async fn log_in_or_restore_session(client: &Client, session_path: &Path) -> Result<()> {
992+
let session_path = session_path.join("session.json");
987993

988994
if let Ok(serialized) = std::fs::read_to_string(&session_path) {
989995
let session: MatrixSession = serde_json::from_str(&serialized)?;
990996
client.restore_session(session).await?;
997+
991998
println!("restored session");
992999
} else {
993-
login_with_password(&client).await?;
1000+
login_with_password(client).await?;
9941001
println!("new login");
9951002

9961003
// Immediately save the session to disk.
9971004
if let Some(session) = client.session() {
9981005
let AuthSession::Matrix(session) = session else { panic!("unexpected oidc session") };
9991006
let serialized = serde_json::to_string(&session)?;
10001007
std::fs::write(session_path, serialized)?;
1008+
10011009
println!("saved session");
10021010
}
10031011
}
10041012

1005-
Ok(client)
1013+
Ok(())
10061014
}
10071015

10081016
/// Asks the user of a username and password, and try to login using the matrix

0 commit comments

Comments
 (0)