master
lion 3 days ago
parent c2bf797cc2
commit c43eec2354

@ -8,14 +8,15 @@ class SupplyDemandAccess
{
use ApiResponse;
public static function registered($user): bool {
return $user && trim((string) $user->name) !== '' && preg_match('/^1\d{10}$/', (string) $user->getRawOriginal('mobile'));
return $user && preg_match('/^1\d{10}$/', (string) $user->getRawOriginal('mobile'));
}
private function tokenUser($bearer) {
if (!is_string($bearer) || $bearer === '') return null;
$token = PersonalAccessToken::findToken($bearer);
if (!$token || !($token->tokenable instanceof User)) return null;
if ($token->expires_at && !\Illuminate\Support\Carbon::parse($token->expires_at)->isFuture()) return null;
if (config('sanctum.expiration') && !$token->created_at->gt(now()->subMinutes(config('sanctum.expiration')))) return null;
// Match the existing mobile login lifecycle; token age alone does not log out a member.
// Revoked tokens and tokens with an explicit expiry remain invalid.
return $token->tokenable;
}
public function handle($request, Closure $next, $mode = 'member') {

@ -230,6 +230,15 @@ class SupplyDemandHandoverTest extends TestCase
$this->withToken('invalid')->getJson('/api/mobile/supply-demand/detail?id='.$d->id)->assertJsonPath('errcode',10002);
}
public function test_existing_mobile_account_with_older_token_does_not_need_login_again() {
$this->enableGuestRequests();config(['sanctum.expiration'=>1440]);
$user=User::find(2);$user->name=null;$user->save();
$token=$user->createToken('mobile-token')->plainTextToken;
DB::table('personal_access_tokens')->update(['created_at'=>now()->subDays(7)]);
$this->withToken($token)->getJson('/api/mobile/supply-demand/session')->assertJsonPath('registered',true)->assertJsonPath('user.id',2);
$this->withToken($token)->getJson('/api/mobile/supply-demand/unread-count')->assertJsonMissing(['errcode'=>40001])->assertJsonMissing(['errcode'=>10003]);
}
public function test_expired_real_token_can_browse_but_cannot_send() {
$d=$this->demand();$this->enableGuestRequests();$token=User::find(2)->createToken('mobile-token')->plainTextToken;
DB::table('personal_access_tokens')->update(['expires_at'=>now()->subMinute()]);

Loading…
Cancel
Save