add console.log tracing to /create/account webhook
This commit is contained in:
@@ -14,26 +14,37 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { email, name, subscription_level } = body;
|
const { email, name, subscription_level } = body;
|
||||||
|
|
||||||
|
console.log('[create/account] received', JSON.stringify({ email, name, subscription_level }));
|
||||||
|
|
||||||
if (!email || typeof email !== 'string') {
|
if (!email || typeof email !== 'string') {
|
||||||
|
console.log('[create/account] missing email');
|
||||||
error(400, 'email is required');
|
error(400, 'email is required');
|
||||||
}
|
}
|
||||||
if (!subscription_level || typeof subscription_level !== 'string') {
|
if (!subscription_level || typeof subscription_level !== 'string') {
|
||||||
|
console.log('[create/account] missing subscription_level');
|
||||||
error(400, 'subscription_level is required');
|
error(400, 'subscription_level is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('[create/account] checking subscription level');
|
||||||
const level = await prisma.subscriptionLevel.findFirst({
|
const level = await prisma.subscriptionLevel.findFirst({
|
||||||
where: { name: subscription_level }
|
where: { name: subscription_level }
|
||||||
});
|
});
|
||||||
if (!level) {
|
if (!level) {
|
||||||
|
console.log('[create/account] subscription_level not found:', subscription_level);
|
||||||
error(400, `subscription_level "${subscription_level}" not found`);
|
error(400, `subscription_level "${subscription_level}" not found`);
|
||||||
}
|
}
|
||||||
|
console.log('[create/account] subscription level found:', level.id, level.name);
|
||||||
|
|
||||||
|
console.log('[create/account] checking for existing user');
|
||||||
let user = await prisma.user.findUnique({ where: { email } });
|
let user = await prisma.user.findUnique({ where: { email } });
|
||||||
|
console.log('[create/account] existing user:', user ? user.id : 'none');
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const tempPassword = crypto.randomUUID().replace(/-/g, '').slice(0, 20);
|
const tempPassword = crypto.randomUUID().replace(/-/g, '').slice(0, 20);
|
||||||
|
console.log('[create/account] generated temp password');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('[create/account] calling signUpEmail for:', email);
|
||||||
await auth.api.signUpEmail({
|
await auth.api.signUpEmail({
|
||||||
body: {
|
body: {
|
||||||
email,
|
email,
|
||||||
@@ -41,9 +52,13 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
name: name ?? email.split('@')[0]
|
name: name ?? email.split('@')[0]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log('[create/account] signUpEmail succeeded');
|
||||||
|
|
||||||
|
console.log('[create/account] fetching new user record');
|
||||||
user = await prisma.user.findUnique({ where: { email } });
|
user = await prisma.user.findUnique({ where: { email } });
|
||||||
|
console.log('[create/account] user fetched:', user?.id);
|
||||||
|
|
||||||
|
console.log('[create/account] querying verification table');
|
||||||
const verification = await prisma.verification.findFirst({
|
const verification = await prisma.verification.findFirst({
|
||||||
where: {
|
where: {
|
||||||
identifier: email,
|
identifier: email,
|
||||||
@@ -51,15 +66,17 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
},
|
},
|
||||||
orderBy: { createdAt: 'desc' }
|
orderBy: { createdAt: 'desc' }
|
||||||
});
|
});
|
||||||
|
console.log('[create/account] verification found:', verification ? verification.id : 'none');
|
||||||
|
|
||||||
const verifyUrl = verification
|
const verifyUrl = verification
|
||||||
? `${BETTER_AUTH_URL}/api/auth/verify-email?token=${verification.value}&callbackURL=/login?verified=true`
|
? `${BETTER_AUTH_URL}/api/auth/verify-email?token=${verification.value}&callbackURL=/login?verified=true`
|
||||||
: `${BETTER_AUTH_URL}/login`;
|
: `${BETTER_AUTH_URL}/login`;
|
||||||
|
console.log('[create/account] verifyUrl:', verifyUrl);
|
||||||
|
|
||||||
await sendMail({
|
console.log('[create/account] sending automated-user-registration email');
|
||||||
|
const regResult = await sendMail({
|
||||||
to: email,
|
to: email,
|
||||||
templateId: 'automated-user-registration',
|
templateId: 'automated-user-registration',
|
||||||
subject: 'Your account is ready',
|
|
||||||
data: {
|
data: {
|
||||||
firstName: name ?? email.split('@')[0],
|
firstName: name ?? email.split('@')[0],
|
||||||
tempPassword,
|
tempPassword,
|
||||||
@@ -67,18 +84,22 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
loginUrl: `${BETTER_AUTH_URL}/login`
|
loginUrl: `${BETTER_AUTH_URL}/login`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log('[create/account] automated-user-registration sent:', JSON.stringify(regResult));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to create user:', err);
|
console.error('[create/account] FAILED - creating user:', err);
|
||||||
error(500, 'Failed to create account');
|
error(500, 'Failed to create account');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
console.log('[create/account] FAILED - no user object');
|
||||||
error(500, 'Failed to find or create user');
|
error(500, 'Failed to find or create user');
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = generateLicenseKey();
|
const key = generateLicenseKey();
|
||||||
|
console.log('[create/account] generated license key:', key);
|
||||||
|
|
||||||
|
console.log('[create/account] saving license key to DB');
|
||||||
await prisma.licenseKey.create({
|
await prisma.licenseKey.create({
|
||||||
data: {
|
data: {
|
||||||
key,
|
key,
|
||||||
@@ -87,21 +108,24 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
userId: user.id
|
userId: user.id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log('[create/account] license key saved');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sendMail({
|
console.log('[create/account] sending license-key-delivery email');
|
||||||
|
const keyResult = await sendMail({
|
||||||
to: email,
|
to: email,
|
||||||
templateId: 'license-key-delivery',
|
templateId: 'license-key-delivery',
|
||||||
subject: 'Your License Key',
|
|
||||||
data: {
|
data: {
|
||||||
firstName: name ?? 'Valued Customer',
|
firstName: name ?? 'Valued Customer',
|
||||||
license_key: key
|
license_key: key
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log('[create/account] license-key-delivery sent:', JSON.stringify(keyResult));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to send license key email:', e);
|
console.error('[create/account] FAILED - sending license key email:', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('[create/account] returning success for user:', user.id);
|
||||||
return json({
|
return json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Account and license key created',
|
message: 'Account and license key created',
|
||||||
|
|||||||
Reference in New Issue
Block a user