diff --git a/server/src/handlers/authenticate.rs b/server/src/handlers/authenticate.rs index 535d99c..1cdedc3 100644 --- a/server/src/handlers/authenticate.rs +++ b/server/src/handlers/authenticate.rs @@ -11,8 +11,7 @@ use crate::{AuthenticateRequest, AuthenticateResponse, ClientState, State, User, pub async fn authenticate(state: Arc>, client_state: Arc>, conn: &mut WsStream, number: u32, req: AuthenticateRequest) -> anyhow::Result<()> { if client_state.read().await.user.is_some() { - util::send(conn, number, AuthenticateResponse::error("already logged in")).await?; - return Ok(()); + return util::send(conn, number, AuthenticateResponse::error("already logged in")).await; } let key = prefixed_api_key::parse(&*req.key) @@ -29,10 +28,7 @@ pub async fn authenticate(state: Arc>, client_state: Arc u, - None => { - util::send(conn, number, AuthenticateResponse::error("invalid key")).await?; - return Ok(()); - } + None => return util::send(conn, number, AuthenticateResponse::error("invalid key")).await, }; if Utc::now().naive_utc().signed_duration_since(user.last_updated) >= Duration::hours(2) { @@ -59,6 +55,13 @@ pub async fn authenticate(state: Arc>, client_state: Arc>, client_state: Arc Result<()> { pub struct ClientState { user: Option, tx: Sender, + shutdown_tx: Sender<()>, pk: Vec, allow_invites: bool, } @@ -340,10 +341,12 @@ impl ClientState { async fn client_loop(state: Arc>, mut conn: WsStream) -> Result<()> { let (tx, mut rx) = tokio::sync::mpsc::channel(10); + let (shutdown_tx, mut shutdown_rx) = tokio::sync::mpsc::channel(1); let client_state = Arc::new(RwLock::new(ClientState { user: None, tx, + shutdown_tx, pk: Default::default(), allow_invites: false, })); @@ -351,6 +354,10 @@ async fn client_loop(state: Arc>, mut conn: WsStream) -> Result<() loop { let res: Result<()> = try { tokio::select! { + _ = shutdown_rx.recv() => { + debug!("break due to new login"); + break; + } msg = rx.recv() => { if let Some(msg) = msg { let encoded = rmp_serde::to_vec(&msg)?;