fix: create new session when the current expires
This commit is contained in:
parent
a1d3c64765
commit
dc059b947d
|
@ -1,4 +1,5 @@
|
|||
use socialvoid::{ClientError, SocialvoidError};
|
||||
use socialvoid_rawclient::AuthenticationError;
|
||||
use socialvoid_rawclient::ErrorKind;
|
||||
use socialvoid_rawclient::ValidationError;
|
||||
|
||||
|
@ -21,13 +22,16 @@ impl std::fmt::Display for MyFriendlyError {
|
|||
match &self.0 {
|
||||
SocialvoidError::RawClient(err) => match &err.kind {
|
||||
ErrorKind::Authentication(err) => {
|
||||
write!(f, "This method needs you to log in.
|
||||
Authentication Error: {:#?}\nIf you are already logged in, then try logging out and logging in again.
|
||||
To log in:
|
||||
socialvoid-cli login
|
||||
To log out:
|
||||
socialvoid-cli logout", err)
|
||||
}
|
||||
match err {
|
||||
AuthenticationError::SessionExpired => write!(f, "The session was expired. Please login again."),
|
||||
_ => write!(f, "This method needs you to log in.
|
||||
Authentication Error: {:#?}\nIf you are already logged in, then try logging out and logging in again.
|
||||
To log in:
|
||||
socialvoid-cli login
|
||||
To log out:
|
||||
socialvoid-cli logout", err)
|
||||
}
|
||||
},
|
||||
ErrorKind::Cdn(err) => {
|
||||
write!(
|
||||
f,
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use socialvoid::session::RegisterRequest;
|
||||
use socialvoid_rawclient::AuthenticationError;
|
||||
use socialvoid_rawclient::ErrorKind;
|
||||
use structopt::StructOpt;
|
||||
|
||||
mod entities;
|
||||
|
@ -33,18 +35,49 @@ async fn main() {
|
|||
};
|
||||
let password = prompt_password("Enter password: ");
|
||||
//TODO: add OTP support
|
||||
match sv.session.authenticate_user(username, password, None).await {
|
||||
Err(err) => {
|
||||
println!(
|
||||
match sv
|
||||
.session
|
||||
.authenticate_user(username.clone(), password.clone(), None)
|
||||
.await
|
||||
{
|
||||
Err(err) => match err.kind {
|
||||
ErrorKind::Authentication(AuthenticationError::SessionExpired) => {
|
||||
println!("Session expired. Creating new session and retrying.");
|
||||
match sv.session.create().await {
|
||||
Ok(_) => {
|
||||
match sv
|
||||
.session
|
||||
.authenticate_user(username, password, None)
|
||||
.await
|
||||
{
|
||||
Err(err) => println!(
|
||||
"Couldn't authenticate the user.\n{}",
|
||||
MyFriendlyError::from(err)
|
||||
),
|
||||
Ok(_) => println!("Done."),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!(
|
||||
"Couldn't create the session.\n{}",
|
||||
MyFriendlyError::from(err)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => println!(
|
||||
"Couldn't authenticate the user.\n{}",
|
||||
MyFriendlyError::from(err)
|
||||
);
|
||||
}
|
||||
),
|
||||
},
|
||||
Ok(_) => {
|
||||
println!("Successfully logged in.");
|
||||
}
|
||||
}
|
||||
}
|
||||
SocialVoidCommand::Logout => {
|
||||
let _ = sv.session.logout().await;
|
||||
}
|
||||
SocialVoidCommand::Register => {
|
||||
if sv.session.authenticated() {
|
||||
panic!(
|
||||
|
@ -253,6 +286,7 @@ enum SocialVoidCommand {
|
|||
Login {
|
||||
username: Option<String>,
|
||||
},
|
||||
Logout,
|
||||
Register,
|
||||
Config {
|
||||
#[structopt(subcommand)]
|
||||
|
|
Loading…
Reference in New Issue