login: Update last_login_at timestamp

This commit is contained in:
Olivier 'reivilibre 2025-06-15 09:44:03 +01:00
parent ff55d1f254
commit 8d7e7b9004
4 changed files with 40 additions and 7 deletions

View File

@ -0,0 +1,15 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE users SET last_login_utc = $1 WHERE user_id = $2",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Timestamp",
"Uuid"
]
},
"nullable": []
},
"hash": "0d211d66bd0e769d095f83e529c2b5a356289b8ed220e569336a4f5909c4ba9d"
}

View File

@ -1,16 +1,17 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO login_sessions (login_session_token_hash, user_id, started_at_utc, xsrf_secret)\n VALUES ($1, $2, NOW(), $3)",
"query": "INSERT INTO login_sessions (login_session_token_hash, user_id, started_at_utc, xsrf_secret)\n VALUES ($1, $2, $3, $4)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Bytea",
"Uuid",
"Timestamp",
"Bytea"
]
},
"nullable": []
},
"hash": "cfc3e3102493b5b385bcab8eb8d4ea73d38cfb32cdf5c34b6cc76738669490fd"
"hash": "d3ca3c38b26b5ab49a027ada2e3973adcd81f3333de2f06f6bf848d08902bee9"
}

View File

@ -312,15 +312,26 @@ impl IdCoopStoreTxn<'_, '_> {
login_session_token_hash: &[u8; LOGIN_SESSION_TOKEN_HASH_BYTES],
user_id: Uuid,
xsrf_secret: &[u8; LOGIN_SESSION_XSRF_SECRET_BYTES],
now: DateTime<Utc>,
) -> eyre::Result<()> {
let now = now.naive_utc();
sqlx::query!(
"INSERT INTO login_sessions (login_session_token_hash, user_id, started_at_utc, xsrf_secret)
VALUES ($1, $2, NOW(), $3)",
login_session_token_hash, user_id, xsrf_secret
VALUES ($1, $2, $3, $4)",
login_session_token_hash, user_id, now, xsrf_secret
)
.execute(&mut **self.txn)
.await
.context("failed to create login session")?;
sqlx::query!(
"UPDATE users SET last_login_utc = $1 WHERE user_id = $2",
now,
user_id
)
.execute(&mut **self.txn)
.await
.context("failed to bump login time")?;
//
Ok(())
}

View File

@ -35,7 +35,7 @@ use crate::{
config::{Configuration, PasswordHashingConfig},
passwords::{check_hash, create_password_hash},
store::IdCoopStore,
utils::RandGen,
utils::{Clock, RandGen},
};
use super::{
@ -361,6 +361,7 @@ pub(crate) async fn post_login(
SecureClientIp(src_ip): SecureClientIp,
Extension(ratelimiters): Extension<Arc<Ratelimiters>>,
Extension(mut randgen): Extension<RandGen>,
Extension(clock): Extension<Clock>,
DesiredLocale(locale): DesiredLocale,
ambient: Ambient,
Form(form_raw): Form<LoginFormRaw>,
@ -491,8 +492,13 @@ pub(crate) async fn post_login(
store
.txn(|mut txn| {
Box::pin(async move {
txn.create_login_session(&login_session_token_hash, user.user_id, &xsrf_secret)
.await
txn.create_login_session(
&login_session_token_hash,
user.user_id,
&xsrf_secret,
clock.now_utc(),
)
.await
})
})
.await