Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dockerfile #26

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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" <[email protected]>'
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_FROM=YABin <[email protected]>

PUBLIC_REGISRATION_ENABLED=true
PUBLIC_URL="http://localhost:5173"
PUBLIC_URL=http://localhost:5173
ORIGIN=http://localhost:5173
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
prisma migrate deploy
prisma --schema=./src/lib/server/prisma/schema.prisma migrate deploy
pm2-runtime process.yml
6 changes: 3 additions & 3 deletions src/lib/server/auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
};

Expand All @@ -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;

Expand Down
24 changes: 8 additions & 16 deletions src/lib/server/email/base.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/email/verify.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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)}`;

Expand Down
4 changes: 2 additions & 2 deletions src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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: [
Expand Down
10 changes: 5 additions & 5 deletions src/routes/(auth)/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
}

Expand Down Expand Up @@ -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' };
Expand Down
4 changes: 2 additions & 2 deletions src/routes/(auth)/register/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { PUBLIC_REGISRATION_ENABLED } from '$env/static/public';
import { env } from '$env/dynamic/public';
import type { ActionData } from './$types';

export let form: ActionData;
Expand All @@ -20,7 +20,7 @@
<div class="text-green-500 text-center">{form.message}</div>
{/if}

{#if PUBLIC_REGISRATION_ENABLED == 'true'}
{#if env.PUBLIC_REGISRATION_ENABLED == 'true'}
<form method="post" class="mt-2 flex flex-col gap-4" use:enhance>
<div>
<label for="username" class="px-2 py-2">Name</label>
Expand Down
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Select from 'svelte-select';
import { encrypt, encryptWithPassword } from '$lib/crypto';
import Hamburger from '$lib/components/Hamburger.svelte';
import { PUBLIC_REGISRATION_ENABLED } from '$env/static/public';
import { env } from '$env/dynamic/public';
import type { PageData } from './$types';
import { DHMToSeconds, secondsToDHM } from '$lib/utils/time';

Expand Down Expand Up @@ -214,7 +214,7 @@
</button>
</div>

{#if PUBLIC_REGISRATION_ENABLED == 'true'}
{#if env.PUBLIC_REGISRATION_ENABLED == 'true'}
<div class="flex flex-row gap-4 mb-4 justify-center">
{#if data.loggedIn}
<a href="/dashboard/settings" class="underline underline-offset-4 py-1">Dashboard</a>
Expand Down
Loading