From d4e25496b6b585e0441ab444cc52eac81f450e3d Mon Sep 17 00:00:00 2001 From: shreyanshgangwar1509 Date: Sat, 24 Aug 2024 00:27:30 +0530 Subject: [PATCH] first commit --- .gitignore | 2 +- README.md | Bin 1497 -> 1535 bytes package-lock.json | 12 ++++- package.json | 3 +- src/lib/dbconnect.ts | 24 ++++++++++ src/model/User.ts | 71 +++++++++++++++++++++++++++++ src/schemas/acceptMessageSchema.ts | 5 ++ src/schemas/messageSchema.ts | 9 ++++ src/schemas/signInSchema.ts | 6 +++ src/schemas/signUpSchema.ts | 14 ++++++ src/schemas/verifySchema.ts | 5 ++ 11 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/lib/dbconnect.ts create mode 100644 src/model/User.ts create mode 100644 src/schemas/acceptMessageSchema.ts create mode 100644 src/schemas/messageSchema.ts create mode 100644 src/schemas/signInSchema.ts create mode 100644 src/schemas/signUpSchema.ts create mode 100644 src/schemas/verifySchema.ts diff --git a/.gitignore b/.gitignore index fd3dbb5..dfd2500 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,7 @@ yarn-error.log* # local env files .env*.local - +/.env # vercel .vercel diff --git a/README.md b/README.md index b011b30b0330a0fe2c25bbae139d4eca7b3ebc1b..f52896c8ba1e15369d3b386f09c7254273ada4e1 100644 GIT binary patch delta 11 Scmcb~{hxcoP1ec1tZD!uJp{-A delta 7 Ocmey*eUp2`O;!L8aszz; diff --git a/package-lock.json b/package-lock.json index 37566a7..fc310b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,8 @@ "next": "14.2.6", "react": "^18", "react-dom": "^18", - "react-hot-toast": "^2.4.1" + "react-hot-toast": "^2.4.1", + "zod": "^3.23.8" }, "devDependencies": { "@types/node": "^20", @@ -5471,6 +5472,15 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "3.23.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/package.json b/package.json index 0bb9e80..070f671 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "next": "14.2.6", "react": "^18", "react-dom": "^18", - "react-hot-toast": "^2.4.1" + "react-hot-toast": "^2.4.1", + "zod": "^3.23.8" }, "devDependencies": { "@types/node": "^20", diff --git a/src/lib/dbconnect.ts b/src/lib/dbconnect.ts new file mode 100644 index 0000000..cfb54bc --- /dev/null +++ b/src/lib/dbconnect.ts @@ -0,0 +1,24 @@ +import mongoose from "mongoose"; + +type ConnectionObject = { + isConnected?:number +} +const connection: ConnectionObject = {} + +export const dbconnect = async (): Promise => { + if (connection.isConnected) { + console.log('Already connected to database'); + return + } + try { + const db = await mongoose.connect(process.env.MONGODB_URI!||'',{}); + connection.isConnected = db.connections[0].readyState + + console.log('DB connected succefully '); + + } catch (error: any) { + + console.log('Somthing went wrong with db connecton falied',error); + process.exit(1) + } +} diff --git a/src/model/User.ts b/src/model/User.ts new file mode 100644 index 0000000..910d86a --- /dev/null +++ b/src/model/User.ts @@ -0,0 +1,71 @@ +import mongoose, { Document, Schema } from "mongoose"; + +export interface Message extends Document{ + content: string, + createdAt:Date, +} +const MessageSchema: Schema = new Schema({ + content: { + type: String, + required:true + }, + createdAt: { + type: Date, + required: true, + default:Date.now + } +}) +export interface User extends Document{ + username: string, + email: string, + password: string, + verifyCode: string, + verifyCOdeExpiry: Date, + isAcceptingMessage: boolean, + isverifiedd:boolean, + message:Message[], +} +const UserSchema: Schema = new Schema({ + email: { + type: String, + required: [true, "email is reuired"], + unique: true, + // email testing + match:[/.+\@.+\..+/,'please use a valid email address '] + }, + + username: { + type: String, + required: [true, "Username is reuired"], + unique: true, + trim:true, + }, + password: { + type: String, + required: [true, "password is reuired"], + }, + verifyCode: { + type: String, + required: [true, "verifycode is reuired"], + }, + verifyCOdeExpiry: { + type: Date, + required: [true, "verifycodeexpiry is reuired"], + }, + isverifiedd: { + type: Boolean, + default:false, + + }, + isAcceptingMessage: { + type: Boolean, + default:true, + }, + message: [MessageSchema] + + +}) + +const UserModel = mongoose.models.User as mongoose.Model || mongoose.model("User",UserSchema) + +export default UserModel; \ No newline at end of file diff --git a/src/schemas/acceptMessageSchema.ts b/src/schemas/acceptMessageSchema.ts new file mode 100644 index 0000000..01e0068 --- /dev/null +++ b/src/schemas/acceptMessageSchema.ts @@ -0,0 +1,5 @@ +import { z } from "zod" + +export const acceptMessageSchema = z.object({ + acceptMessages:z.boolean(), +}) \ No newline at end of file diff --git a/src/schemas/messageSchema.ts b/src/schemas/messageSchema.ts new file mode 100644 index 0000000..5829352 --- /dev/null +++ b/src/schemas/messageSchema.ts @@ -0,0 +1,9 @@ + +import { z } from "zod" + +export const MessageSchema = z.object({ + content: z + .string() + .min(10, 'content must be atleast of 10 character ') + .max(300,'content must be atmost of 300 character '), +}) \ No newline at end of file diff --git a/src/schemas/signInSchema.ts b/src/schemas/signInSchema.ts new file mode 100644 index 0000000..7f58b86 --- /dev/null +++ b/src/schemas/signInSchema.ts @@ -0,0 +1,6 @@ +import { z } from "zod" + +export const SignInSchema = z.object({ + identifier: z.string(), + password:z.string() +}) \ No newline at end of file diff --git a/src/schemas/signUpSchema.ts b/src/schemas/signUpSchema.ts new file mode 100644 index 0000000..225d534 --- /dev/null +++ b/src/schemas/signUpSchema.ts @@ -0,0 +1,14 @@ +import { z } from 'zod' + +export const usernameValidation = z + .string() + .min(2, "Username must be atleast 2 charcaters") + .max(20, "Usernaem must be at most 20 character") + .regex(/^[a-zA-Z0-9_]+$/, "Usrname must not conatain special cahracter ") + +export const signUpSchema = z.object({ + username: usernameValidation, + email: z.string().email({message:'Invalid email address'}), + password: z.string().min(6, { message:"password must be atleast 6 character"}) +}) + diff --git a/src/schemas/verifySchema.ts b/src/schemas/verifySchema.ts new file mode 100644 index 0000000..9f2b4e1 --- /dev/null +++ b/src/schemas/verifySchema.ts @@ -0,0 +1,5 @@ +import { z } from "zod" + +export const verifySchema = z.object({ + code:z.string().length(6,'verification code of 6 digits') +}) \ No newline at end of file