1- use chrono:: { NaiveDateTime , Utc } ;
1+ use chrono:: Utc ;
22use num_traits:: FromPrimitive ;
33use rocket:: {
44 form:: { Form , FromForm } ,
@@ -147,7 +147,7 @@ async fn _refresh_login(data: ConnectData, conn: &DbConn, ip: &ClientIp) -> Json
147147 }
148148 Ok ( ( mut device, auth_tokens) ) => {
149149 // Save to update `device.updated_at` to track usage and toggle new status
150- device. save ( conn) . await ?;
150+ device. save ( true , conn) . await ?;
151151
152152 let result = json ! ( {
153153 "refresh_token" : auth_tokens. refresh_token( ) ,
@@ -267,9 +267,10 @@ async fn _sso_login(
267267 }
268268 Some ( ( mut user, sso_user) ) => {
269269 let mut device = get_device ( & data, conn, & user) . await ?;
270-
271- // Save to update `device.updated_at` to track usage and toggle new status
272- device. save ( conn) . await ?;
270+ if !device. is_new ( ) {
271+ // Update `device.updated_at` only if it's not a new device
272+ device. save ( true , conn) . await ?;
273+ }
273274
274275 let twofactor_token = twofactor_auth ( & mut user, & data, & mut device, ip, client_version, conn) . await ?;
275276
@@ -317,7 +318,7 @@ async fn _sso_login(
317318 auth_user. expires_in ,
318319 ) ?;
319320
320- authenticated_response ( & user, & mut device, auth_tokens, twofactor_token, & now , conn, ip) . await
321+ authenticated_response ( & user, & mut device, auth_tokens, twofactor_token, conn, ip) . await
321322}
322323
323324async fn _password_login (
@@ -434,28 +435,29 @@ async fn _password_login(
434435 }
435436
436437 let mut device = get_device ( & data, conn, & user) . await ?;
437-
438- // Save to update `device.updated_at` to track usage and toggle new status
439- device. save ( conn) . await ?;
438+ if !device. is_new ( ) {
439+ // Update `device.updated_at` only if it's not a new device
440+ device. save ( true , conn) . await ?;
441+ }
440442
441443 let twofactor_token = twofactor_auth ( & mut user, & data, & mut device, ip, client_version, conn) . await ?;
442444
443445 let auth_tokens = auth:: AuthTokens :: new ( & device, & user, AuthMethod :: Password , data. client_id ) ;
444446
445- authenticated_response ( & user, & mut device, auth_tokens, twofactor_token, & now , conn, ip) . await
447+ authenticated_response ( & user, & mut device, auth_tokens, twofactor_token, conn, ip) . await
446448}
447449
448450async fn authenticated_response (
449451 user : & User ,
450452 device : & mut Device ,
451453 auth_tokens : auth:: AuthTokens ,
452454 twofactor_token : Option < String > ,
453- now : & NaiveDateTime ,
454455 conn : & DbConn ,
455456 ip : & ClientIp ,
456457) -> JsonResult {
457458 if CONFIG . mail_enabled ( ) && device. is_new ( ) {
458- if let Err ( e) = mail:: send_new_device_logged_in ( & user. email , & ip. ip . to_string ( ) , now, device) . await {
459+ let now = Utc :: now ( ) . naive_utc ( ) ;
460+ if let Err ( e) = mail:: send_new_device_logged_in ( & user. email , & ip. ip . to_string ( ) , & now, device) . await {
459461 error ! ( "Error sending new device email: {e:#?}" ) ;
460462
461463 if CONFIG . require_device_email ( ) {
@@ -475,7 +477,7 @@ async fn authenticated_response(
475477 }
476478
477479 // Save to update `device.updated_at` to track usage and toggle new status
478- device. save ( conn) . await ?;
480+ device. save ( true , conn) . await ?;
479481
480482 let master_password_policy = master_password_policy ( user, conn) . await ;
481483
@@ -592,7 +594,7 @@ async fn _user_api_key_login(
592594 let access_claims = auth:: LoginJwtClaims :: default ( & device, & user, & AuthMethod :: UserApiKey , data. client_id ) ;
593595
594596 // Save to update `device.updated_at` to track usage and toggle new status
595- device. save ( conn) . await ?;
597+ device. save ( true , conn) . await ?;
596598
597599 info ! ( "User {} logged in successfully via API key. IP: {}" , user. email, ip. ip) ;
598600
@@ -655,7 +657,12 @@ async fn get_device(data: &ConnectData, conn: &DbConn, user: &User) -> ApiResult
655657 // Find device or create new
656658 match Device :: find_by_uuid_and_user ( & device_id, & user. uuid , conn) . await {
657659 Some ( device) => Ok ( device) ,
658- None => Device :: new ( device_id, user. uuid . clone ( ) , device_name, device_type, conn) . await ,
660+ None => {
661+ let mut device = Device :: new ( device_id, user. uuid . clone ( ) , device_name, device_type) ;
662+ // save device without updating `device.updated_at`
663+ device. save ( false , conn) . await ?;
664+ Ok ( device)
665+ }
659666 }
660667}
661668
0 commit comments