diff --git a/.env.example b/.env.example index 472572e..8b39abe 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,14 @@ -DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public" +DATABASE_URL=postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public SALT="this is a very insecure salt, change it" MAIL_ENABLED=false -MAIL_SERVER="smtp.gmail.com" +MAIL_SERVER=smtp.gmail.com MAIL_PORT=465 MAIL_USE_SSL=true -MAIL_USERNAME="" -MAIL_PASSWORD="" -MAIL_FROM='"YABin" ' +MAIL_USERNAME= +MAIL_PASSWORD= +MAIL_FROM=YABin PUBLIC_REGISRATION_ENABLED=true -PUBLIC_URL="http://localhost:5173" +PUBLIC_URL=http://localhost:5173 +ORIGIN=http://localhost:5173 diff --git a/Dockerfile b/Dockerfile index 334221c..45bc8f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,8 @@ FROM node:18-alpine AS builder WORKDIR /app -COPY package.json yarn.lock src/lib/server/prisma/schema.prisma ./ +COPY package.json yarn.lock ./ +COPY src/lib/server/prisma/ src/lib/server/prisma/ RUN yarn install --frozen-lockfile @@ -19,7 +20,7 @@ RUN npm install -g prisma pm2 COPY scripts/ scripts/ COPY package.json yarn.lock process.yml ./ -COPY src/lib/server/prisma/ prisma/ +COPY src/lib/server/prisma/ src/lib/server/prisma/ RUN yarn install --frozen-lockfile --production diff --git a/package.json b/package.json index 3179e70..e47b353 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "lint": "prettier --plugin-search-dir . --check . && eslint .", "format": "prettier --plugin-search-dir . --write .", + "prepare": "svelte-kit sync", "postinstall": "prisma generate --schema=./src/lib/server/prisma/schema.prisma" }, "devDependencies": { diff --git a/scripts/run.sh b/scripts/run.sh index e6a2c21..a921d5b 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -prisma migrate deploy +prisma --schema=./src/lib/server/prisma/schema.prisma migrate deploy pm2-runtime process.yml diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index 2eb893d..146c8ef 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -1,4 +1,4 @@ -import { SALT } from '$env/static/private'; +import { env } from '$env/dynamic/private'; import { hashPassword } from '$lib/crypto'; import prisma from '@db'; import type { Cookies } from '@sveltejs/kit'; @@ -21,7 +21,7 @@ export const generateVerificationHash = async (userId: string) => { const user = await prisma.user.findUnique({ where: { id: userId } }); if (!user) throw new Error('User not found'); - const hash = await hashPassword(`${user.email}${user.id}${user.password}${user.verified}`, SALT); + const hash = await hashPassword(`${user.email}${user.id}${user.password}${user.verified}`, env.SALT); return hash; }; @@ -31,7 +31,7 @@ export const validateVerificationHash = async (userId: string, hash: string) => const newHash = await hashPassword( `${user.email}${user.id}${user.password}${user.verified}`, - SALT + env.SALT ); if (newHash !== hash) return false; diff --git a/src/lib/server/email/base.ts b/src/lib/server/email/base.ts index 7d032ad..1ad9485 100644 --- a/src/lib/server/email/base.ts +++ b/src/lib/server/email/base.ts @@ -1,31 +1,23 @@ import nodemailer from 'nodemailer'; -import { - MAIL_ENABLED, - MAIL_SERVER, - MAIL_PASSWORD, - MAIL_PORT, - MAIL_USERNAME, - MAIL_USE_SSL, - MAIL_FROM -} from '$env/static/private'; +import { env } from '$env/dynamic/private'; export async function sendEmail(to: string, subject: string, content: string) { - if (MAIL_ENABLED !== 'true') { + if (env.MAIL_ENABLED !== 'true') { return false; } const transporter = nodemailer.createTransport({ - host: MAIL_SERVER, - port: Number(MAIL_PORT), - secure: MAIL_USE_SSL === 'true', + host: env.MAIL_SERVER, + port: Number(env.MAIL_PORT), + secure: env.MAIL_USE_SSL === 'true', auth: { - user: MAIL_USERNAME, - pass: MAIL_PASSWORD + user: env.MAIL_USERNAME, + pass: env.MAIL_PASSWORD } }); const info = await transporter.sendMail({ - from: MAIL_FROM, + from: env.MAIL_FROM, to, subject, text: content diff --git a/src/lib/server/email/verify.ts b/src/lib/server/email/verify.ts index 44a5a7a..a585536 100644 --- a/src/lib/server/email/verify.ts +++ b/src/lib/server/email/verify.ts @@ -1,4 +1,4 @@ -import { PUBLIC_URL } from '$env/static/public'; +import { env } from '$env/dynamic/public'; import prisma from '@db'; import { generateVerificationHash } from '../auth'; import { sendEmail } from './base'; @@ -9,7 +9,7 @@ export const sendVerificationEmail = async (userId: string) => { const hash = await generateVerificationHash(userId); - const verifyUrl = `${PUBLIC_URL}/validate?hash=${encodeURIComponent( + const verifyUrl = `${env.PUBLIC_URL}/validate?hash=${encodeURIComponent( hash )}&userId=${encodeURIComponent(userId)}`; diff --git a/src/routes/(auth)/login/+page.server.ts b/src/routes/(auth)/login/+page.server.ts index 4218826..0b46029 100644 --- a/src/routes/(auth)/login/+page.server.ts +++ b/src/routes/(auth)/login/+page.server.ts @@ -3,7 +3,7 @@ import { fail, redirect } from '@sveltejs/kit'; import prisma from '@db'; import { hashPassword } from '$lib/crypto'; import { nanoid } from 'nanoid'; -import { SALT } from '$env/static/private'; +import { env } from '$env/dynamic/private'; export const actions: Actions = { default: async ({ cookies, request }) => { @@ -16,7 +16,7 @@ export const actions: Actions = { return fail(400, { success: false, errors: ['All fields are required'] }); } - const hashedPassword = await hashPassword(password.toString(), SALT); + const hashedPassword = await hashPassword(password.toString(), env.SALT); const user = await prisma.user.findFirst({ where: { OR: [ diff --git a/src/routes/(auth)/register/+page.server.ts b/src/routes/(auth)/register/+page.server.ts index 1c8b655..f714d29 100644 --- a/src/routes/(auth)/register/+page.server.ts +++ b/src/routes/(auth)/register/+page.server.ts @@ -3,15 +3,15 @@ import { fail, redirect } from '@sveltejs/kit'; import prisma from '@db'; import { hashPassword } from '$lib/crypto'; import { nanoid } from 'nanoid'; -import { MAIL_ENABLED, SALT } from '$env/static/private'; -import { PUBLIC_REGISRATION_ENABLED } from '$env/static/public'; +import { env } from '$env/dynamic/private'; +import { env as envPublic } from '$env/dynamic/public'; import { sendVerificationEmail } from '$lib/server/email/verify'; const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g; export const actions: Actions = { default: async ({ cookies, request }) => { - if (PUBLIC_REGISRATION_ENABLED !== 'true') { + if (envPublic.PUBLIC_REGISRATION_ENABLED !== 'true') { return fail(404, { success: false, errors: ['Not found'] }); } @@ -70,12 +70,12 @@ export const actions: Actions = { name: name.toString(), username: username.toString(), email: email.toString(), - password: await hashPassword(password.toString(), SALT), + password: await hashPassword(password.toString(), env.SALT), verified: false } }); - if (MAIL_ENABLED === 'true') { + if (env.MAIL_ENABLED === 'true') { const sentVerificationEmail = await sendVerificationEmail(user.id); if (sentVerificationEmail) { return { success: true, message: 'Please check your e-mail for verification link' }; diff --git a/src/routes/(auth)/register/+page.svelte b/src/routes/(auth)/register/+page.svelte index ea8a784..4f92f83 100644 --- a/src/routes/(auth)/register/+page.svelte +++ b/src/routes/(auth)/register/+page.svelte @@ -1,6 +1,6 @@