Add internal grant endpoint + temp-password / force-change flow

- /api/internal/grant: secret-auth endpoint for payment-router to
  provision access on a paid order — find-or-create user (emailVerified,
  temp password, mustChangePassword), enroll by course slug list,
  AccessLog, delivery email. Idempotent by order_id.
- User.mustChangePassword flag (+ migration); (student) layout redirects
  flagged users to /change-password (forced first-login change).
- email.ts: sendCourseGrantEmail (temp password for new buyers).
- middleware: /api/internal is public (own secret auth).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-14 16:25:35 +05:00
parent 5a67ac8086
commit a7abf454d9
11 changed files with 293 additions and 1 deletions
+44
View File
@@ -213,6 +213,50 @@ export async function sendAccessEmail(
}).catch((e) => console.error("[email] sendAccessEmail:", e));
}
// Выдача доступа по факту оплаты (через payment-router /api/internal/grant).
// tempPassword != null — новый покупатель (аккаунт создан с временным паролем,
// force-change при первом входе). null — существующий (курсы добавлены).
export async function sendCourseGrantEmail(
to: string,
name: string,
courseTitles: string[],
tempPassword: string | null,
) {
const school = await getSchoolName();
const list = courseTitles
.map((t) => `<li style="margin:0 0 6px;">${t}</li>`)
.join("");
const loginBlock = tempPassword
? `
<p ${p}><strong>Данные для входа:</strong></p>
${quote(`Адрес: school.second-brain.ru\nEmail: ${to}\nПароль: ${tempPassword}`)}
<p ${p}>Это <strong>временный пароль</strong> — при первом входе система попросит задать свой.</p>`
: `
<p ${p}>Войдите под своим обычным паролем — новые курсы уже на главной.</p>`;
const subject =
courseTitles.length > 1
? `Доступ к курсам ${school} открыт`
: `Доступ к курсу «${courseTitles[0] ?? school}» открыт`;
await getResend()
.emails.send({
from: FROM,
to,
subject,
html: base(
`
<p ${p}>Привет, ${name || ""} 👋🏽</p>
<p ${p}>Спасибо за покупку! Доступ уже открыт в вашем личном кабинете на <strong>school.second-brain.ru</strong>. Вам открыты курсы:</p>
<ul style="margin:0 0 18px;padding-left:20px;font-family:Arial,Helvetica,sans-serif;font-size:14px;line-height:1.65;color:#323232;">${list}</ul>
${loginBlock}
${btn(`${BASE_URL}/login`, "Войти на платформу")}
<p style="margin:18px 0 0;font-family:Arial,Helvetica,sans-serif;font-size:14px;line-height:1.65;color:#666666;">Если что-то не получается — просто ответьте на это письмо.</p>
`,
school,
),
})
.catch((e) => console.error("[email] sendCourseGrantEmail:", e));
}
export async function sendPasswordResetEmail(to: string, name: string, resetUrl: string) {
const school = await getSchoolName();
await getResend().emails.send({