Skip to content

Commit

Permalink
zk: auth user register email create email pass body
Browse files Browse the repository at this point in the history
  • Loading branch information
zobkazi committed Apr 28, 2024
1 parent fb87886 commit 3589fc3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
47 changes: 37 additions & 10 deletions packages/services/auth/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ enum AccountStatus {
}

model AuthUser {
id String @id @default(cuid())
name String
email String @unique
password String
role Role @default(USER)
verified Boolean @default(false)
status AccountStatus @default(PENDING)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
LoginHistory LoginHistory[]
id String @id @default(cuid())
name String
email String @unique
password String
role Role @default(USER)
verified Boolean @default(false)
status AccountStatus @default(PENDING)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
LoginHistory LoginHistory[]
VerificationCode VerificationCode[]
}

enum LoginAttempt {
Expand All @@ -51,3 +52,29 @@ model LoginHistory {
userAgent String?
attempt LoginAttempt @default(SUCCESS)
}

enum VerificationStatus {
PENDING
USER
EXPIRED
}

enum VerificationCodeType {
ACCOUND_ACITVATION
FORGOT_PASSWORD
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?
}
13 changes: 13 additions & 0 deletions packages/services/auth/src/controllers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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";

const UserRegister = async (
req: Request,
Expand Down Expand Up @@ -52,6 +53,18 @@ const UserRegister = async (

console.log(user);



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

return res.status(201).json({
success: true,
data: user,
Expand Down
15 changes: 15 additions & 0 deletions packages/services/auth/src/utils/generateVerificationCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const generateVerificationCode = () => {
// Get current timestamp in milliseconds
const timestamp = new Date().getTime().toString();

// Generate a random 2-digit number
const randomNum = Math.floor(10 + Math.random() * 90); // Ensures 2-digit random number

// Combine timestamp and random number and extract last 5 digits
let code = (timestamp + randomNum).slice(-5);

return code;

}

export default generateVerificationCode
13 changes: 10 additions & 3 deletions packages/services/email/src/controllers/getEmails.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import prisma from "@/utils/prisma"
import { Request, Response,NextFunction } from "express"

const getEmail = async (req: Request, res: Response, next: NextFunction) => {

const getEmails = async (req: Request, res: Response, next: NextFunction) => {
try {
const emails = await prisma.email.findMany()

res.status(200).json(emails)
} catch (error) {
next(error)
}
}

export default getEmail
export default getEmails

0 comments on commit 3589fc3

Please sign in to comment.