2026-02-11 19:28:12 +01:00
< ? php
namespace MintyPHP\Repository\Auth ;
use MintyPHP\DB ;
2026-03-13 21:58:51 +01:00
/** Manages persistent login tokens with rotation, family tracking, and admin-initiated expiry. */
2026-03-05 08:26:51 +01:00
class RememberTokenRepository implements RememberTokenRepositoryInterface
2026-02-11 19:28:12 +01:00
{
2026-02-23 12:58:19 +01:00
private function unwrapList ( $rows ) : array
2026-02-11 19:28:12 +01:00
{
if ( ! is_array ( $rows )) {
return [];
}
$list = [];
foreach ( $rows as $row ) {
$data = $row [ 'user_remember_tokens' ] ? ? $row ;
if ( is_array ( $data )) {
$list [] = $data ;
}
}
return $list ;
}
2026-02-23 12:58:19 +01:00
public function create ( int $userId , string $selector , string $tokenHash , string $expiresAt ) : ? int
2026-02-11 19:28:12 +01:00
{
$id = DB :: insert (
'insert into user_remember_tokens (user_id, selector, token_hash, expires_at, created) values (?,?,?,?,NOW())' ,
( string ) $userId ,
$selector ,
$tokenHash ,
$expiresAt
);
return $id ? ( int ) $id : null ;
}
2026-02-23 12:58:19 +01:00
public function findBySelector ( string $selector ) : ? array
2026-02-11 19:28:12 +01:00
{
$row = DB :: selectOne (
'select id, user_id, selector, token_hash, expires_at, expired_by_admin_at, last_used from user_remember_tokens where selector = ? limit 1' ,
$selector
);
if ( ! $row || ! isset ( $row [ 'user_remember_tokens' ])) {
return null ;
}
return $row [ 'user_remember_tokens' ];
}
2026-02-23 12:58:19 +01:00
public function updateToken ( int $id , string $tokenHash , string $expiresAt ) : bool
2026-02-11 19:28:12 +01:00
{
$result = DB :: update (
'update user_remember_tokens set token_hash = ?, expires_at = ?, expired_by_admin_at = NULL, last_used = NOW() where id = ?' ,
$tokenHash ,
$expiresAt ,
( string ) $id
);
return $result !== false ;
}
2026-02-23 12:58:19 +01:00
public function expireAllByAdmin () : int
2026-02-11 19:28:12 +01:00
{
$result = DB :: update (
'update user_remember_tokens set expires_at = NOW(), expired_by_admin_at = NOW() where expires_at > NOW()'
);
return $result !== false ? ( int ) $result : 0 ;
}
2026-02-23 12:58:19 +01:00
public function deleteById ( int $id ) : bool
2026-02-11 19:28:12 +01:00
{
$result = DB :: delete ( 'delete from user_remember_tokens where id = ?' , ( string ) $id );
return $result !== false ;
}
2026-02-23 12:58:19 +01:00
public function deleteByUserId ( int $userId ) : bool
2026-02-11 19:28:12 +01:00
{
$result = DB :: delete ( 'delete from user_remember_tokens where user_id = ?' , ( string ) $userId );
return $result !== false ;
}
2026-02-23 12:58:19 +01:00
public function listByUserId ( int $userId , int $limit = 20 ) : array
2026-02-11 19:28:12 +01:00
{
if ( $userId <= 0 ) {
return [];
}
if ( $limit < 1 ) {
$limit = 20 ;
} elseif ( $limit > 100 ) {
$limit = 100 ;
}
$rows = DB :: select (
'select id, user_id, selector, expires_at, expired_by_admin_at, last_used, created from user_remember_tokens where user_id = ? order by id desc limit ?' ,
( string ) $userId ,
( string ) $limit
);
2026-02-23 12:58:19 +01:00
return $this -> unwrapList ( $rows );
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function countActive () : int
2026-02-11 19:28:12 +01:00
{
$count = DB :: selectValue ( 'select count(*) from user_remember_tokens where expires_at > NOW()' );
return $count ? ( int ) $count : 0 ;
}
}