feat: update auth service to provision databases via TenantConnectionManager
- Replace createTenantSchema with tenantDb.provisionDatabase - JWT payload now includes databaseName (already renamed from schemaName) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { prisma } from '../config/database.js';
|
import { prisma, tenantDb } from '../config/database.js';
|
||||||
import { hashPassword, verifyPassword } from '../utils/password.js';
|
import { hashPassword, verifyPassword } from '../utils/password.js';
|
||||||
import { generateAccessToken, generateRefreshToken, verifyToken } from '../utils/token.js';
|
import { generateAccessToken, generateRefreshToken, verifyToken } from '../utils/token.js';
|
||||||
import { createTenantSchema } from '../utils/schema-manager.js';
|
|
||||||
import { AppError } from '../middlewares/error.middleware.js';
|
import { AppError } from '../middlewares/error.middleware.js';
|
||||||
import { PLANS } from '@horux/shared';
|
import { PLANS } from '@horux/shared';
|
||||||
import type { LoginRequest, RegisterRequest, LoginResponse } from '@horux/shared';
|
import type { LoginRequest, RegisterRequest, LoginResponse } from '@horux/shared';
|
||||||
@@ -23,7 +22,8 @@ export async function register(data: RegisterRequest): Promise<LoginResponse> {
|
|||||||
throw new AppError(400, 'El RFC ya está registrado');
|
throw new AppError(400, 'El RFC ya está registrado');
|
||||||
}
|
}
|
||||||
|
|
||||||
const databaseName = `horux_${data.empresa.rfc.toLowerCase().replace(/[^a-z0-9]/g, '')}`;
|
// Provision a dedicated database for this tenant
|
||||||
|
const databaseName = await tenantDb.provisionDatabase(data.empresa.rfc);
|
||||||
|
|
||||||
const tenant = await prisma.tenant.create({
|
const tenant = await prisma.tenant.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -36,8 +36,6 @@ export async function register(data: RegisterRequest): Promise<LoginResponse> {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await createTenantSchema(databaseName);
|
|
||||||
|
|
||||||
const passwordHash = await hashPassword(data.usuario.password);
|
const passwordHash = await hashPassword(data.usuario.password);
|
||||||
const user = await prisma.user.create({
|
const user = await prisma.user.create({
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user