diff --git a/routes/ponshu.js b/routes/ponshu.js index b83a2ed3..33bf9932 100644 --- a/routes/ponshu.js +++ b/routes/ponshu.js @@ -40,12 +40,28 @@ module.exports = function createPonshuRouter(pool, authMiddleware) { }); } - // 初回アクティベート + // 初回アクティベート — WHERE device_id IS NULL で競合を防ぐ if (!license.device_id) { - await pool.query( - `UPDATE ponshu_licenses SET device_id = $1, activated_at = NOW() WHERE license_key = $2`, + const activateResult = await pool.query( + `UPDATE ponshu_licenses SET device_id = $1, activated_at = NOW() + WHERE license_key = $2 AND device_id IS NULL + RETURNING license_key`, [device_id, license_key] ); + if (activateResult.rowCount === 0) { + // 別リクエストが先にアクティベートした → 再取得して照合 + const retry = await pool.query( + `SELECT device_id FROM ponshu_licenses WHERE license_key = $1`, [license_key] + ); + const current = retry.rows[0]; + if (!current || current.device_id !== device_id) { + return res.json({ + valid: false, + error: '別のデバイスで登録済みです。端末変更の場合はサポートまでご連絡ください。', + supportEmail: APP_SUPPORT_EMAIL, + }); + } + } console.log(`[Ponshu] License activated: ${license_key.substring(0, 12)}... -> Device: ${device_id.substring(0, 8)}...`); return res.json({ valid: true, plan: license.plan, activated: true }); }