Skip to content

Commit

Permalink
zk: auth and email faield now
Browse files Browse the repository at this point in the history
  • Loading branch information
zobkazi committed Apr 28, 2024
1 parent 3589fc3 commit cd016e4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ CREATE TYPE "AccountStatus" AS ENUM ('PENDING', 'ACTIVE', 'INACTIVE', 'SUSPENDED
-- CreateEnum
CREATE TYPE "LoginAttempt" AS ENUM ('SUCCESS', 'FAILED');

-- CreateEnum
CREATE TYPE "VerificationStatus" AS ENUM ('PENDING', 'USER', 'EXPIRED');

-- CreateEnum
CREATE TYPE "VerificationCodeType" AS ENUM ('ACCOUND_ACITVATION', 'FORGOT_PASSWORD', 'EMAIL_CHANGE');

-- CreateTable
CREATE TABLE "AuthUser" (
"id" TEXT NOT NULL,
Expand All @@ -29,12 +35,30 @@ CREATE TABLE "LoginHistory" (
"ip" TEXT,
"userAgent" TEXT,
"attempt" "LoginAttempt" NOT NULL DEFAULT 'SUCCESS',
"loggedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "LoginHistory_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "verificationCode" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"code" TEXT NOT NULL,
"type" "VerificationCodeType" NOT NULL DEFAULT 'ACCOUND_ACITVATION',
"status" "VerificationStatus" NOT NULL DEFAULT 'PENDING',
"expireAt" TIMESTAMP(3) NOT NULL,
"issuedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"verifiedAt" TIMESTAMP(3),

CONSTRAINT "verificationCode_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "AuthUser_email_key" ON "AuthUser"("email");

-- AddForeignKey
ALTER TABLE "LoginHistory" ADD CONSTRAINT "LoginHistory_userId_fkey" FOREIGN KEY ("userId") REFERENCES "AuthUser"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "verificationCode" ADD CONSTRAINT "verificationCode_userId_fkey" FOREIGN KEY ("userId") REFERENCES "AuthUser"("id") ON DELETE CASCADE ON UPDATE CASCADE;
25 changes: 12 additions & 13 deletions packages/services/auth/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ model AuthUser {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
LoginHistory LoginHistory[]
VerificationCode VerificationCode[]
VerificationCode verificationCode[]
}

enum LoginAttempt {
Expand All @@ -51,6 +51,7 @@ model LoginHistory {
ip String?
userAgent String?
attempt LoginAttempt @default(SUCCESS)
loggedAt DateTime @default(now())
}

enum VerificationStatus {
Expand All @@ -65,16 +66,14 @@ enum VerificationCodeType {
EMAIL_CHANGE
}

model VerificationCode {
id String @id @default(cuid())
userId String
user AuthUser @relation(fields: [userId], references: [id], onDelete: Cascade)
code String
type VerificationCodeType
status VerificationStatus
expireAt DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
veriedAt DateTime?
model verificationCode {
id String @id @default(cuid())
userId String
user AuthUser @relation(fields: [userId], references: [id], onDelete: Cascade)
code String
type VerificationCodeType @default(ACCOUND_ACITVATION)
status VerificationStatus @default(PENDING)
expireAt DateTime
issuedAt DateTime @default(now())
verifiedAt DateTime?
}
23 changes: 18 additions & 5 deletions packages/services/auth/src/controllers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Request, Response, NextFunction } from "express";
import prisma from "@/utils/prisma";
import { registerSchema } from "../utils/schemas";
import bcrypt from "bcryptjs";
import { generateVerificationCode } from "../utils/generateVerificationCode";
import generateVerificationCode from "@/utils/generateVerificationCode";
import axios from "axios";



const UserRegister = async (
req: Request,
Expand Down Expand Up @@ -56,13 +59,23 @@ const UserRegister = async (


// generate verification code
const verificationCode = generateVerificationCode();
const code = generateVerificationCode();
await prisma.verificationCode.create({
data: {
userId: user.id,
code: verificationCode,
createdAt: new Date(Date.now() + 1000 * 60 *60 * 24), // 1 day
}
code,
expireAt: new Date(Date.now() + 5 * 60 * 1000), // 5 minutes
}
})

// send verification code

await axios.post('http:localhost:4003/emails/send', {
recipient: user.email,
subject: 'Verify your email',
body: `Your verification code is: ${code}`,
source: "user registration",

})

return res.status(201).json({
Expand Down
6 changes: 6 additions & 0 deletions packages/services/auth/src/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ export const LoginHistorySchema = z.object({
export const AccessTokenSchema = z.object({
accessToken: z.string(),
});


export const emailVerificationSchema = z.object({
code: z.string(),
email: z.string().email(),
})

0 comments on commit cd016e4

Please sign in to comment.