-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
980 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
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_PORT=465 | ||
MAIL_USE_SSL=true | ||
MAIL_USERNAME="" | ||
MAIL_PASSWORD="" | ||
MAIL_FROM='"YABin" <[email protected]>' | ||
|
||
PUBLIC_REGISRATION_ENABLED=true | ||
PUBLIC_URL="http://localhost:5173" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { SALT } from '$env/static/private'; | ||
import { hashPassword } from '$lib/crypto'; | ||
import prisma from '@db'; | ||
import type { Cookies } from '@sveltejs/kit'; | ||
|
||
export const getUserIdFromCookie = async (cookies: Cookies) => { | ||
const token = cookies.get('token'); | ||
if (!token) return null; | ||
|
||
const authToken = await prisma.authToken.findFirst({ | ||
where: { token, expiresAt: { gt: new Date() } }, | ||
include: { user: { select: { id: true, verified: true } } } | ||
}); | ||
if (!authToken) return null; | ||
if (!authToken.user.verified) return null; | ||
|
||
return authToken.user.id; | ||
}; | ||
|
||
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); | ||
return hash; | ||
}; | ||
|
||
export const validateVerificationHash = async (userId: string, hash: string) => { | ||
const user = await prisma.user.findUnique({ where: { id: userId } }); | ||
if (!user) return false; | ||
|
||
const newHash = await hashPassword( | ||
`${user.email}${user.id}${user.password}${user.verified}`, | ||
SALT | ||
); | ||
if (newHash !== hash) return false; | ||
|
||
await prisma.user.update({ where: { id: userId }, data: { verified: true } }); | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import nodemailer from 'nodemailer'; | ||
import { | ||
MAIL_ENABLED, | ||
MAIL_SERVER, | ||
MAIL_PASSWORD, | ||
MAIL_PORT, | ||
MAIL_USERNAME, | ||
MAIL_USE_SSL, | ||
MAIL_FROM | ||
} from '$env/static/private'; | ||
|
||
export async function sendEmail(to: string, subject: string, content: string) { | ||
if (MAIL_ENABLED !== 'true') { | ||
return false; | ||
} | ||
|
||
const transporter = nodemailer.createTransport({ | ||
host: MAIL_SERVER, | ||
port: Number(MAIL_PORT), | ||
secure: MAIL_USE_SSL === 'true', | ||
auth: { | ||
user: MAIL_USERNAME, | ||
pass: MAIL_PASSWORD | ||
} | ||
}); | ||
|
||
const info = await transporter.sendMail({ | ||
from: MAIL_FROM, | ||
to, | ||
subject, | ||
text: content | ||
}); | ||
|
||
if (info.accepted.length === 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PUBLIC_URL } from '$env/static/public'; | ||
import prisma from '@db'; | ||
import { generateVerificationHash } from '../auth'; | ||
import { sendEmail } from './base'; | ||
|
||
export const sendVerificationEmail = async (userId: string) => { | ||
const user = await prisma.user.findUnique({ where: { id: userId } }); | ||
if (!user) return false; | ||
|
||
const hash = await generateVerificationHash(userId); | ||
|
||
const verifyUrl = `${PUBLIC_URL}/validate?hash=${encodeURIComponent( | ||
hash | ||
)}&userId=${encodeURIComponent(userId)}`; | ||
|
||
const content = `To verify your email, please click the following link: ${verifyUrl}`; | ||
const subject = 'YABin: Verify your email'; | ||
|
||
const sent = await sendEmail(user.email, subject, content); | ||
if (!sent) return false; | ||
|
||
return true; | ||
}; |
22 changes: 22 additions & 0 deletions
22
src/lib/server/prisma/migrations/20231008132503_add_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- AlterTable | ||
ALTER TABLE "Paste" ADD COLUMN "ownerId" BIGINT; | ||
|
||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" BIGSERIAL NOT NULL, | ||
"username" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"password" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Paste" ADD CONSTRAINT "Paste_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
16 changes: 16 additions & 0 deletions
16
src/lib/server/prisma/migrations/20231008143438_add_authtoken/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- CreateTable | ||
CREATE TABLE "AuthToken" ( | ||
"id" BIGSERIAL NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"expiresAt" TIMESTAMP(3) NOT NULL, | ||
"token" TEXT NOT NULL, | ||
"userId" BIGINT NOT NULL, | ||
|
||
CONSTRAINT "AuthToken_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "AuthToken_token_key" ON "AuthToken"("token"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "AuthToken" ADD CONSTRAINT "AuthToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
5 changes: 5 additions & 0 deletions
5
src/lib/server/prisma/migrations/20231008151119_set_ondelete/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- DropForeignKey | ||
ALTER TABLE "AuthToken" DROP CONSTRAINT "AuthToken_userId_fkey"; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "AuthToken" ADD CONSTRAINT "AuthToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
36 changes: 36 additions & 0 deletions
36
src/lib/server/prisma/migrations/20231008193745_use_nanoid/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Warnings: | ||
- The primary key for the `AuthToken` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "AuthToken" DROP CONSTRAINT "AuthToken_userId_fkey"; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE "Paste" DROP CONSTRAINT "Paste_ownerId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "AuthToken" DROP CONSTRAINT "AuthToken_pkey", | ||
ALTER COLUMN "id" DROP DEFAULT, | ||
ALTER COLUMN "id" SET DATA TYPE TEXT, | ||
ALTER COLUMN "userId" SET DATA TYPE TEXT, | ||
ADD CONSTRAINT "AuthToken_pkey" PRIMARY KEY ("id"); | ||
DROP SEQUENCE "AuthToken_id_seq"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Paste" ALTER COLUMN "ownerId" SET DATA TYPE TEXT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP CONSTRAINT "User_pkey", | ||
ALTER COLUMN "id" DROP DEFAULT, | ||
ALTER COLUMN "id" SET DATA TYPE TEXT, | ||
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id"); | ||
DROP SEQUENCE "User_id_seq"; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "AuthToken" ADD CONSTRAINT "AuthToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Paste" ADD CONSTRAINT "Paste_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
2 changes: 2 additions & 0 deletions
2
src/lib/server/prisma/migrations/20231008201146_add_verified_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "verified" BOOLEAN NOT NULL DEFAULT false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.