Merge branch 'deviceid-login' into 'master'

Handle optional device_id field during login

See merge request famedly/conduit!16
This commit is contained in:
Timo Kösters 2021-02-07 12:28:05 +00:00
commit 3588dcd6d0
2 changed files with 19 additions and 10 deletions

View file

@ -77,7 +77,6 @@ pub async fn login_route(
// Generate new device id if the user didn't specify one // Generate new device id if the user didn't specify one
let device_id = body let device_id = body
.body
.device_id .device_id
.clone() .clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); .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 // Generate a new token for the device
let token = utils::random_string(TOKEN_LENGTH); let token = utils::random_string(TOKEN_LENGTH);
// TODO: Don't always create a new device // Determine if device_id was provided and exists in the db for this user
// Add device let device_exists = body.device_id.as_ref().map_or(false, |device_id| {
db.users.create_device( db.users
&user_id, .all_device_ids(&user_id)
&device_id, .find(|x| x.as_ref().map_or(false, |v| v == device_id))
&token, .is_some()
body.initial_device_display_name.clone(), });
)?;
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); info!("{} logged in", user_id);

View file

@ -252,7 +252,7 @@ impl Users {
} }
/// Replaces the access token of one device. /// 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(); let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff); userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes()); userdeviceid.extend_from_slice(device_id.as_bytes());