Fix login code delivery mode

This commit is contained in:
Ruslan Bakiev
2026-05-16 09:23:40 +07:00
parent 47ba203edc
commit c641a3dd23
2 changed files with 25 additions and 38 deletions

View File

@@ -14,3 +14,4 @@ SMTP_SECURE=false
SMTP_USER=
SMTP_PASS=
SMTP_FROM=
AUTH_LOGIN_CODE_DELIVERY=email

View File

@@ -30,6 +30,16 @@ import { fetchTelegramConnectionProfile } from './telegram.js';
const ACTIVE_ORDER_STATUSES = ['NEW', 'MANAGER_PROCESSING', 'WAITING_DOUBLE_CONFIRM', 'CONFIRMED', 'IN_PROGRESS'];
function getLoginCodeDeliveryMode() {
const mode = String(process.env.AUTH_LOGIN_CODE_DELIVERY ?? 'email')
.trim()
.toLowerCase();
if (mode !== 'email' && mode !== 'static') {
throw new Error('AUTH_LOGIN_CODE_DELIVERY must be either "email" or "static".');
}
return mode;
}
function toFloat(value) {
return value == null ? null : Number(value);
}
@@ -320,20 +330,6 @@ function invitationToken() {
return crypto.randomBytes(24).toString('hex');
}
function buildDefaultFullName(email) {
const localPart = email.split('@')[0]?.trim();
if (!localPart) {
return 'Новый пользователь';
}
return localPart
.replace(/[._-]+/g, ' ')
.split(' ')
.filter(Boolean)
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join(' ');
}
function normalizeText(value) {
return String(value ?? '').trim();
}
@@ -1435,19 +1431,24 @@ export const resolvers = {
},
},
});
if (!user) {
throw new Error('User is not invited to the cabinet.');
}
const challenge = createLoginChallenge({
userId: user?.id ?? null,
userId: user.id,
channel: input.channel,
destination,
});
const code = getStaticAuthCode();
await sendLoginCodeEmail({
to: destination,
code,
expiresAt: challenge.expiresAt,
});
if (getLoginCodeDeliveryMode() === 'email') {
await sendLoginCodeEmail({
to: destination,
code,
expiresAt: challenge.expiresAt,
});
}
return {
challengeToken: challenge.challengeToken,
@@ -1463,24 +1464,9 @@ export const resolvers = {
code: input.code,
});
let user = challenge.userId
? await context.prisma.user.findUnique({
where: { id: challenge.userId },
})
: null;
if (!user && challenge.channel === 'EMAIL') {
const email = String(challenge.destination).trim().toLowerCase();
user = await context.prisma.user.upsert({
where: { email },
update: {},
create: {
email,
fullName: buildDefaultFullName(email),
role: 'CLIENT',
},
});
}
const user = await context.prisma.user.findUnique({
where: { id: challenge.userId },
});
if (!user) {
throw new Error('User is not available for this login challenge.');