Skip to content

Commit

Permalink
fix: migrate all the rows
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <[email protected]>
  • Loading branch information
mattkrick committed Jul 25, 2024
1 parent 6fd49d8 commit d171d43
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 169 deletions.
28 changes: 0 additions & 28 deletions packages/server/database/types/EmailVerification.ts

This file was deleted.

23 changes: 13 additions & 10 deletions packages/server/email/createEmailVerficationForExistingUser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import base64url from 'base64url'
import crypto from 'crypto'
import getKysely from '../postgres/getKysely'
import {Threshold} from 'parabol-client/types/constEnums'
import AuthIdentityLocal from '../database/types/AuthIdentityLocal'
import EmailVerification from '../database/types/EmailVerification'
import {DataLoaderWorker} from '../graphql/graphql'
import getKysely from '../postgres/getKysely'
import emailVerificationEmailCreator from './emailVerificationEmailCreator'
import getMailManager from './getMailManager'

Expand Down Expand Up @@ -34,15 +34,18 @@ const createEmailVerficationForExistingUser = async (
if (!success) {
return new Error('Unable to send verification email')
}
const emailVerification = new EmailVerification({
email,
token: verifiedEmailToken,
hashedPassword,
pseudoId,
invitationToken
})
const pg = getKysely()
await pg.insertInto('EmailVerification').values(emailVerification).execute()
await pg
.insertInto('EmailVerification')
.values({
email,
token: verifiedEmailToken,
hashedPassword,
pseudoId,
invitationToken,
expiration: new Date(Date.now() + Threshold.EMAIL_VERIFICATION_LIFESPAN)
})
.execute()
return undefined
}

Expand Down
22 changes: 12 additions & 10 deletions packages/server/email/createEmailVerification.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import base64url from 'base64url'
import bcrypt from 'bcryptjs'
import crypto from 'crypto'
import {Security} from 'parabol-client/types/constEnums'
import {Security, Threshold} from 'parabol-client/types/constEnums'
import getKysely from '../postgres/getKysely'
import EmailVerification from '../database/types/EmailVerification'
import emailVerificationEmailCreator from './emailVerificationEmailCreator'
import getMailManager from './getMailManager'

Expand Down Expand Up @@ -36,15 +35,18 @@ const createEmailVerification = async (props: SignUpWithPasswordMutationVariable
return {error: {message: 'Unable to send verification email'}}
}
const hashedPassword = await bcrypt.hash(password, Security.SALT_ROUNDS)
const emailVerification = new EmailVerification({
email,
token: verifiedEmailToken,
hashedPassword,
pseudoId,
invitationToken
})
const pg = getKysely()
await pg.insertInto('EmailVerification').values(emailVerification).execute()
await pg
.insertInto('EmailVerification')
.values({
email,
token: verifiedEmailToken,
hashedPassword,
pseudoId,
invitationToken,
expiration: new Date(Date.now() + Threshold.EMAIL_VERIFICATION_LIFESPAN)
})
.execute()
return {error: {message: 'Verification required. Check your inbox.'}}
}

Expand Down
13 changes: 6 additions & 7 deletions packages/server/graphql/public/mutations/signUpWithPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ const signUpWithPassword: MutationResolvers['signUpWithPassword'] = async (
}
const verificationRequired = await isEmailVerificationRequired(email, dataLoader)
if (verificationRequired) {
const existingVerification =
(await pg
.selectFrom('EmailVerification')
.selectAll()
.where('email', '=', email)
.where('expiration', '>', new Date())
.executeTakeFirst()) || null
const existingVerification = await pg
.selectFrom('EmailVerification')
.selectAll()
.where('email', '=', email)
.where('expiration', '>', new Date())
.executeTakeFirst()
if (existingVerification) {
return {error: {message: 'Verification email already sent'}}
}
Expand Down
12 changes: 5 additions & 7 deletions packages/server/graphql/public/mutations/verifyEmail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {AuthIdentityTypeEnum} from '../../../../client/types/constEnums'
import AuthIdentityLocal from '../../../database/types/AuthIdentityLocal'
import AuthToken from '../../../database/types/AuthToken'
import EmailVerification from '../../../database/types/EmailVerification'
import getKysely from '../../../postgres/getKysely'
import {getUserByEmail} from '../../../postgres/queries/getUsersByEmails'
import updateUser from '../../../postgres/queries/updateUser'
Expand All @@ -18,12 +17,11 @@ const verifyEmail: MutationResolvers['verifyEmail'] = async (
const {dataLoader} = context
const pg = getKysely()
const now = new Date()
const emailVerification =
((await pg
.selectFrom('EmailVerification')
.selectAll()
.where('token', '=', verificationToken)
.executeTakeFirst()) as EmailVerification) || null
const emailVerification = await pg
.selectFrom('EmailVerification')
.selectAll()
.where('token', '=', verificationToken)
.executeTakeFirst()

if (!emailVerification) {
return {error: {message: 'Invalid verification token'}}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {Kysely, PostgresDialect, sql} from 'kysely'
import {Client} from 'pg'
import {r} from 'rethinkdb-ts'
import connectRethinkDB from '../../database/connectRethinkDB'
import getPg from '../getPg'
import getPgConfig from '../getPgConfig'

export async function up() {
await connectRethinkDB()
const pg = new Kysely<any>({
dialect: new PostgresDialect({
pool: getPg()
})
})
await sql`
CREATE TABLE "EmailVerification" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"email" "citext" NOT NULL,
"expiration" TIMESTAMP WITH TIME ZONE NOT NULL,
"token" VARCHAR(100) NOT NULL,
"hashedPassword" VARCHAR(100),
"invitationToken" VARCHAR(100),
"pseudoId" VARCHAR(100)
);
CREATE INDEX IF NOT EXISTS "idx_EmailVerification_email" ON "EmailVerification"("email");
CREATE INDEX IF NOT EXISTS "idx_EmailVerification_token" ON "EmailVerification"("token");
`.execute(pg)

const rData = await r.table('EmailVerification').coerceTo('array').run()
const insertData = rData.map((row) => {
const {email, expiration, hashedPassword, token, invitationToken, pseudoId} = row
return {
email,
expiration,
hashedPassword,
token,
invitationToken,
pseudoId
}
})
await pg.insertInto('EmailVerification').values(insertData).execute()
}

export async function down() {
const client = new Client(getPgConfig())
await client.connect()
await client.query(`
DROP TABLE IF EXISTS "EmailVerification";
`)
await client.end()
}

0 comments on commit d171d43

Please sign in to comment.