diff --git a/src/client_server/session.rs b/src/client_server/session.rs index da3d8d88..48fbea2a 100644 --- a/src/client_server/session.rs +++ b/src/client_server/session.rs @@ -77,7 +77,6 @@ pub async fn login_route( // Generate new device id if the user didn't specify one let device_id = body - .body .device_id .clone() .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); @@ -85,14 +84,24 @@ pub async fn login_route( // Generate a new token for the device let token = utils::random_string(TOKEN_LENGTH); - // TODO: Don't always create a new device - // Add device - db.users.create_device( - &user_id, - &device_id, - &token, - body.initial_device_display_name.clone(), - )?; + // Determine if device_id was provided and exists in the db for this user + let device_exists = body.device_id.as_ref().map_or(false, |device_id| { + db.users + .all_device_ids(&user_id) + .find(|x| x.as_ref().map_or(false, |v| v == device_id)) + .is_some() + }); + + if device_exists { + db.users.set_token(&user_id, &device_id, &token)?; + } else { + db.users.create_device( + &user_id, + &device_id, + &token, + body.initial_device_display_name.clone(), + )?; + } info!("{} logged in", user_id); diff --git a/src/database/users.rs b/src/database/users.rs index 0421ae22..9da0776b 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -252,7 +252,7 @@ impl Users { } /// Replaces the access token of one device. - fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> { + pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); userdeviceid.extend_from_slice(device_id.as_bytes());