new selfhosted version

This commit is contained in:
antonio
2025-11-28 14:11:51 +01:00
parent afda29997d
commit 951860f67e
1046 changed files with 72586 additions and 574750 deletions

View File

@@ -1,47 +1,36 @@
import crypto from 'node:crypto';
import { z } from "zod";
import { RegisterModel } from '~/shared/schema/RegisterSchema';
import { UserModel } from '~/shared/schema/UserSchema';
import { createRegisterJwt } from '~/server/AuthManager';
import { UserModel } from '@schema/UserSchema';
import { RegisterModel } from '@schema/RegisterSchema';
import { EmailService } from '@services/EmailService';
import crypto from 'crypto';
import { EmailServiceHelper } from '~/server/services/EmailServiceHelper';
const ZRegisterBody = z.object({
email: z.string().email(),
password: z.string().min(6).max(64)
});
function canRegister(email: string, password: string) {
if (email.length == 0) return false;
if (!email.includes('@')) return false;
if (!email.includes('.')) return false;
if (password.length < 6) return false;
return true;
};
export default defineEventHandler(async (event) => {
export default defineEventHandler(async event => {
//TODO: SELFHOST
const { email, password } = await readValidatedBody(event, ZRegisterBody.parse);
const { email, password } = await readBody(event);
const user = await UserModel.exists({ email });
if (user) throw createError({ statusCode: 400, message: 'Email already registered' });
if (!canRegister(email, password)) return setResponseStatus(event, 400, 'Email or Password not match criteria');
const hashedPassword = await hashPassword(password);
const user = await UserModel.findOne({ email });
const code = crypto.randomBytes(3).toString('hex').toUpperCase();
if (user) return {
error: true,
message: 'Email already registered'
}
await RegisterModel.updateOne({ email }, { password: hashedPassword, code }, { upsert: true });
const hash = crypto.createHash('sha256');
const hashedPassword = hash.update(password + '_litlyx').digest('hex');
const { BASE_URL } = useRuntimeConfig();
const jwt = createRegisterJwt(email, hashedPassword);
await RegisterModel.create({ email, password: hashedPassword });
const link = `${BASE_URL}/api/auth/confirm_email?code=${code}`;
setImmediate(() => {
const emailData = EmailService.getEmailServerInfo('confirm', { target: email, link: `https://dashboard.litlyx.com/api/auth/confirm_email?register_code=${jwt}` });
EmailServiceHelper.sendEmail(emailData);
const tRpc = useTRPC();
tRpc.emails.email.sendConfirmEmail.mutate({ email, link });
});
return {
error: false,
message: 'OK'
}
});
return { ok: true };
});