mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-12-29 10:14:32 +01:00
improvement: Handle optional device_id field during login
remove debug logging
This commit is contained in:
parent
9424ba0559
commit
890187e004
2 changed files with 27 additions and 10 deletions
|
@ -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,32 @@ 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
|
let mut create_new_device = true;
|
||||||
// Add device
|
|
||||||
|
// Only search db for existing device if one was provided in the request
|
||||||
|
match &body.device_id {
|
||||||
|
Some(_) => {
|
||||||
|
// Look to see if provided device_id already exists
|
||||||
|
if let Some(_) = db.users.all_device_ids(&user_id).find(|x| match x {
|
||||||
|
Ok(x) if **x == *device_id => true,
|
||||||
|
_ => false,
|
||||||
|
}) {
|
||||||
|
// Replace token for existing device
|
||||||
|
db.users.set_token(&user_id, &device_id, &token)?;
|
||||||
|
create_new_device = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
|
||||||
|
if create_new_device {
|
||||||
db.users.create_device(
|
db.users.create_device(
|
||||||
&user_id,
|
&user_id,
|
||||||
&device_id,
|
&device_id,
|
||||||
&token,
|
&token,
|
||||||
body.initial_device_display_name.clone(),
|
body.initial_device_display_name.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
info!("{} logged in", user_id);
|
info!("{} logged in", user_id);
|
||||||
|
|
||||||
|
|
|
@ -251,7 +251,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());
|
||||||
|
|
Loading…
Reference in a new issue