Fix jwt-token

This commit is contained in:
2025-09-13 21:09:59 +02:00
parent 4e234c0bfc
commit a6c3b6aaf1
3 changed files with 149 additions and 46 deletions

View File

@@ -279,6 +279,33 @@ class MultiTenantAuth {
{ expiresIn: '24h' }
);
}
/**
* Determine tenant from request (slug or subdomain)
*/
async determineTenant(req) {
try {
// Try to get tenant from subdomain first
const host = req.get('host') || '';
const subdomain = host.split('.')[0];
// If subdomain is not localhost/IP, use it as tenant slug
if (subdomain && !subdomain.match(/^(localhost|127\.0\.0\.1|\d+\.\d+\.\d+\.\d+)$/)) {
return subdomain;
}
// Fallback: get from user's tenant if authenticated
if (req.user && req.user.tenant_id) {
const tenant = await Tenant.findByPk(req.user.tenant_id);
return tenant ? tenant.slug : null;
}
return null;
} catch (error) {
console.error('Error determining tenant:', error);
return null;
}
}
}
module.exports = MultiTenantAuth;