-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Added support for changing email of users (#233)
- Loading branch information
1 parent
f16162a
commit 5ea9a10
Showing
10 changed files
with
482 additions
and
26 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
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,34 @@ | ||
import { Otp, PrismaClient, User } from '@prisma/client' | ||
|
||
const OTP_EXPIRY = 5 * 60 * 1000 // 5 minutes | ||
|
||
export default async function generateOtp( | ||
email: User['email'], | ||
userId: User['id'], | ||
prisma: PrismaClient | ||
): Promise<Otp> { | ||
const otp = await prisma.otp.upsert({ | ||
where: { | ||
userId: userId | ||
}, | ||
update: { | ||
code: BigInt(`0x${crypto.randomUUID().replace(/-/g, '')}`) | ||
.toString() | ||
.substring(0, 6), | ||
expiresAt: new Date(new Date().getTime() + OTP_EXPIRY) | ||
}, | ||
create: { | ||
code: BigInt(`0x${crypto.randomUUID().replace(/-/g, '')}`) | ||
.toString() | ||
.substring(0, 6), | ||
expiresAt: new Date(new Date().getTime() + OTP_EXPIRY), | ||
user: { | ||
connect: { | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return otp | ||
} |
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
24 changes: 24 additions & 0 deletions
24
apps/api/src/prisma/migrations/20240521090751_add_user_email_change/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,24 @@ | ||
/* | ||
Warnings: | ||
- The required column `id` was added to the `Otp` table with a prisma-level default value. This is not possible if the table is not empty. Please add this column as optional, then populate it before making it required. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Otp" ADD COLUMN "id" TEXT NOT NULL, | ||
ADD CONSTRAINT "Otp_pkey" PRIMARY KEY ("id"); | ||
|
||
-- CreateTable | ||
CREATE TABLE "UserEmailChange" ( | ||
"id" TEXT NOT NULL, | ||
"otpId" TEXT NOT NULL, | ||
"newEmail" TEXT NOT NULL, | ||
|
||
CONSTRAINT "UserEmailChange_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "UserEmailChange_otpId_key" ON "UserEmailChange"("otpId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "UserEmailChange" ADD CONSTRAINT "UserEmailChange_otpId_fkey" FOREIGN KEY ("otpId") REFERENCES "Otp"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
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.