-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from amitamrutiya/prisma
Prisma
- Loading branch information
Showing
25 changed files
with
317 additions
and
621 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
45 changes: 45 additions & 0 deletions
45
frontend/prisma/migrations/20240612103723_initialize/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,45 @@ | ||
-- CreateTable | ||
CREATE TABLE "users" ( | ||
"id" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"password" TEXT NOT NULL, | ||
"username" TEXT NOT NULL, | ||
"fullname" TEXT NOT NULL, | ||
"profile_image" TEXT, | ||
"bio" TEXT NOT NULL DEFAULT 'Hey there! I am available on ChatApp', | ||
"phone_number" TEXT NOT NULL, | ||
"friends" TEXT[], | ||
"is_online" BOOLEAN NOT NULL DEFAULT false, | ||
"is_verified" BOOLEAN NOT NULL DEFAULT false, | ||
"verifyCode" TEXT NOT NULL, | ||
"verifyCodeExpiry" TIMESTAMP(3) NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "users_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "chats" ( | ||
"id" SERIAL NOT NULL, | ||
"senderId" INTEGER NOT NULL, | ||
"receiverId" INTEGER NOT NULL, | ||
"message" TEXT NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL, | ||
"userId" TEXT, | ||
|
||
CONSTRAINT "chats_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users_username_key" ON "users"("username"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users_phone_number_key" ON "users"("phone_number"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "chats" ADD CONSTRAINT "chats_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "users" ALTER COLUMN "phone_number" DROP NOT NULL, | ||
ALTER COLUMN "phone_number" SET DEFAULT '0000000000'; |
2 changes: 2 additions & 0 deletions
2
frontend/prisma/migrations/20240612114524_remove_default_phone/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 "users" ALTER COLUMN "phone_number" DROP DEFAULT; |
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,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
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,48 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? | ||
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id String @id @default(uuid()) | ||
email String @unique | ||
password String | ||
username String @unique | ||
fullname String | ||
profile_image String? | ||
bio String @default("Hey there! I am available on ChatApp") | ||
phone_number String? @unique | ||
friends String[] | ||
is_online Boolean @default(false) | ||
is_verified Boolean @default(false) | ||
verifyCode String | ||
verifyCodeExpiry DateTime | ||
chats Chat[] | ||
createdAt DateTime @default(now()) @map("created_at") | ||
updatedAt DateTime @updatedAt @map("updated_at") | ||
@@map("users") | ||
} | ||
|
||
model Chat { | ||
id Int @id @default(autoincrement()) | ||
senderId Int | ||
receiverId Int | ||
message String | ||
createdAt DateTime @default(now()) @map("created_at") | ||
updatedAt DateTime @updatedAt @map("updated_at") | ||
User User? @relation(fields: [userId], references: [id]) | ||
userId String? | ||
@@map("chats") | ||
} |
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,33 @@ | ||
"use server"; | ||
|
||
import { db } from "@/lib/db"; | ||
import { usernameValidation } from "@/schemas/profileSchema"; | ||
import { z } from "zod"; | ||
|
||
const UsernameQuerySchema = z.object({ | ||
username: usernameValidation, | ||
}); | ||
|
||
export async function checkUsernameUnique( | ||
values: z.infer<typeof UsernameQuerySchema> | ||
) { | ||
const validatedFields = UsernameQuerySchema.safeParse(values); | ||
|
||
if (!validatedFields.success) { | ||
return { success: false, message: validatedFields.error.message }; | ||
} | ||
const { username } = validatedFields.data; | ||
|
||
const existingVerifiedUser = await db.user.findFirst({ | ||
where: { | ||
username, | ||
is_verified: true, | ||
}, | ||
}); | ||
|
||
if (existingVerifiedUser) { | ||
return { success: false, message: "Username already exists" }; | ||
} | ||
|
||
return { success: true, message: "username is unique" }; | ||
} |
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,84 @@ | ||
"use server"; | ||
|
||
import { db } from "@/lib/db"; | ||
import { signUpSchema } from "@/schemas/signupSchema"; | ||
import { z } from "zod"; | ||
import bcrypt from "bcryptjs"; | ||
import { User } from "@prisma/client"; | ||
import { sendVerificationEmail } from "@/helpers/sendVerificationEmail"; | ||
|
||
export async function register(values: z.infer<typeof signUpSchema>) { | ||
const validatedFields = signUpSchema.safeParse(values); | ||
|
||
if (!validatedFields.success) { | ||
return { success: false, message: validatedFields.error.message }; | ||
} | ||
const { username, email, password, fullname } = validatedFields.data; | ||
|
||
const existingUserVerifiedByUsername = await db.user.findFirst({ | ||
where: { | ||
username, | ||
is_verified: true, | ||
}, | ||
}); | ||
|
||
if (existingUserVerifiedByUsername) { | ||
return { success: false, message: "Username already exists" }; | ||
} | ||
|
||
const existingUserByEmail = await db.user.findFirst({ | ||
where: { | ||
email, | ||
}, | ||
}); | ||
const salt = await bcrypt.genSalt(10); | ||
const hashedPassword = await bcrypt.hash(password, salt); | ||
const expiryDate = new Date(); | ||
expiryDate.setMinutes(expiryDate.getMinutes() + 10); | ||
const verifyCode = Math.floor(100000 + Math.random() * 900000).toString(); | ||
|
||
if (existingUserByEmail && existingUserByEmail.is_verified) { | ||
existingUserByEmail.password = hashedPassword; | ||
existingUserByEmail.verifyCode = verifyCode; | ||
existingUserByEmail.verifyCodeExpiry = expiryDate; | ||
existingUserByEmail.updatedAt = new Date(); | ||
await db.user.create({ | ||
data: existingUserByEmail, | ||
}); | ||
return { success: false, message: "User already exists with this email" }; | ||
} else { | ||
const firstName = fullname.split(" ")[0]; | ||
const lastName = fullname.split(" ")[1] ?? ""; | ||
const newUser = await db.user.create({ | ||
data: { | ||
email, | ||
fullname, | ||
password: hashedPassword, | ||
username: username, | ||
profile_image: `https://api.dicebear.com/5.x/initials/svg?seed=${firstName}+${lastName}`, | ||
is_online: true, | ||
is_verified: false, | ||
verifyCode: verifyCode, | ||
verifyCodeExpiry: expiryDate, | ||
friends: [], | ||
chats: undefined, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
}); | ||
|
||
const emailResponse = await sendVerificationEmail( | ||
email, | ||
username, | ||
verifyCode | ||
); | ||
|
||
if (!emailResponse.success) { | ||
return { success: false, message: "Error sending verification email" }; | ||
} | ||
return { | ||
success: true, | ||
message: "User registered successfully. Please verify your email", | ||
}; | ||
} | ||
} |
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,49 @@ | ||
"use server"; | ||
|
||
import { db } from "@/lib/db"; | ||
import { verifySchema } from "@/schemas/verifySchema"; | ||
import { z } from "zod"; | ||
|
||
type verifyCodeType = { | ||
username: string; | ||
code: z.infer<typeof verifySchema>; | ||
}; | ||
|
||
export async function verifyCode(value: verifyCodeType) { | ||
const validatedFields = verifySchema.safeParse(value.code); | ||
|
||
if (!validatedFields.success) { | ||
return { success: false, message: validatedFields.error.message }; | ||
} | ||
const { code } = validatedFields.data; | ||
const username = value.username; | ||
|
||
const user = await db.user.findFirst({ | ||
where: { | ||
username, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
return { success: false, message: "Invalid email or verification code" }; | ||
} | ||
|
||
const isCodeValid = user.verifyCode === code; | ||
const isCodeExpired = new Date(user.verifyCodeExpiry) > new Date(); | ||
|
||
if (isCodeValid && isCodeExpired) { | ||
await db.user.update({ | ||
where: { | ||
id: user.id, | ||
}, | ||
data: { | ||
is_verified: true, | ||
}, | ||
}); | ||
|
||
return { success: true, message: "User verified successfully" }; | ||
} else if (!isCodeExpired) { | ||
return { success: false, message: "Verification code has expired" }; | ||
} | ||
return { success: false, message: "Invalid verification code" }; | ||
} |
Oops, something went wrong.