|
1 | 1 | use std::{
|
2 | 2 | collections::HashMap,
|
3 | 3 | io::{self, stdout, Write},
|
4 |
| - path::PathBuf, |
| 4 | + path::{Path, PathBuf}, |
5 | 5 | sync::{Arc, Mutex},
|
6 | 6 | time::Duration,
|
7 | 7 | };
|
@@ -983,26 +983,34 @@ async fn configure_client(cli: Cli) -> Result<Client> {
|
983 | 983 | let client = client_builder.build().await?;
|
984 | 984 |
|
985 | 985 | // 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"); |
987 | 993 |
|
988 | 994 | if let Ok(serialized) = std::fs::read_to_string(&session_path) {
|
989 | 995 | let session: MatrixSession = serde_json::from_str(&serialized)?;
|
990 | 996 | client.restore_session(session).await?;
|
| 997 | + |
991 | 998 | println!("restored session");
|
992 | 999 | } else {
|
993 |
| - login_with_password(&client).await?; |
| 1000 | + login_with_password(client).await?; |
994 | 1001 | println!("new login");
|
995 | 1002 |
|
996 | 1003 | // Immediately save the session to disk.
|
997 | 1004 | if let Some(session) = client.session() {
|
998 | 1005 | let AuthSession::Matrix(session) = session else { panic!("unexpected oidc session") };
|
999 | 1006 | let serialized = serde_json::to_string(&session)?;
|
1000 | 1007 | std::fs::write(session_path, serialized)?;
|
| 1008 | + |
1001 | 1009 | println!("saved session");
|
1002 | 1010 | }
|
1003 | 1011 | }
|
1004 | 1012 |
|
1005 |
| - Ok(client) |
| 1013 | + Ok(()) |
1006 | 1014 | }
|
1007 | 1015 |
|
1008 | 1016 | /// Asks the user of a username and password, and try to login using the matrix
|
|
0 commit comments