ФУНКЦИИ - NODEDC LAUNCHER: регистрация по инвайту
This commit is contained in:
@@ -361,6 +361,52 @@ app.get("/api/invites/:token", (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/api/invites/:token/register", asyncRoute(async (req, res) => {
|
||||
let payload;
|
||||
|
||||
try {
|
||||
payload = sanitizeInviteRegistrationPayload(req.body);
|
||||
} catch (error) {
|
||||
sendInviteApiError(res, error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!authentikSyncClient.isConfigured()) {
|
||||
res.status(503).json({ error: "Регистрация временно недоступна: Authentik API не настроен" });
|
||||
return;
|
||||
}
|
||||
|
||||
let draft;
|
||||
|
||||
try {
|
||||
draft = controlPlaneStore.prepareInviteRegistration(req.params.token, payload);
|
||||
} catch (error) {
|
||||
sendInviteApiError(res, error);
|
||||
return;
|
||||
}
|
||||
|
||||
const provisionedUser = await authentikSyncClient.provisionUser({
|
||||
data: draft.data,
|
||||
userId: draft.user.id,
|
||||
password: payload.password,
|
||||
});
|
||||
const result = await controlPlaneStore.commitInviteRegistration(req.params.token, payload, provisionedUser);
|
||||
const storeResult = await controlPlaneStore.markUserAuthentikProvisioned(result.user.id, provisionedUser, {
|
||||
sub: provisionedUser.authentikUserId,
|
||||
email: result.user.email,
|
||||
name: result.user.name,
|
||||
});
|
||||
|
||||
publishControlPlaneEvent("invite.registered", [result.user.id]);
|
||||
res.json({
|
||||
...result,
|
||||
user: storeResult.user,
|
||||
data: storeResult.data,
|
||||
provisioning: toProvisioningResponse(provisionedUser),
|
||||
loginUrl: buildLoginRedirectUrl("/", { forceLogin: true }),
|
||||
});
|
||||
}));
|
||||
|
||||
app.post("/api/invites/:token/accept", requireSession, asyncRoute(async (req, res) => {
|
||||
let result;
|
||||
|
||||
@@ -783,6 +829,15 @@ function sanitizeNewPassword(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function sanitizeInviteRegistrationPayload(payload) {
|
||||
const name = typeof payload?.name === "string" ? payload.name.trim() : "";
|
||||
|
||||
return {
|
||||
name: name.slice(0, 120),
|
||||
password: sanitizeNewPassword(payload?.password),
|
||||
};
|
||||
}
|
||||
|
||||
function sendInviteApiError(res, error) {
|
||||
const message = error instanceof Error ? error.message : "Инвайт недоступен";
|
||||
const status =
|
||||
|
||||
Reference in New Issue
Block a user