Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyanshgangwar1509 committed Aug 23, 2024
1 parent 2c88958 commit d4e2549
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ yarn-error.log*

# local env files
.env*.local

/.env
# vercel
.vercel

Expand Down
Binary file modified README.md
Binary file not shown.
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions src/lib/dbconnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose from "mongoose";

type ConnectionObject = {
isConnected?:number
}
const connection: ConnectionObject = {}

export const dbconnect = async (): Promise<void> => {
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)
}
}
71 changes: 71 additions & 0 deletions src/model/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import mongoose, { Document, Schema } from "mongoose";

export interface Message extends Document{
content: string,
createdAt:Date,
}
const MessageSchema: Schema<Message> = 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<User> = 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<User> || mongoose.model<User>("User",UserSchema)

export default UserModel;
5 changes: 5 additions & 0 deletions src/schemas/acceptMessageSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod"

export const acceptMessageSchema = z.object({
acceptMessages:z.boolean(),
})
9 changes: 9 additions & 0 deletions src/schemas/messageSchema.ts
Original file line number Diff line number Diff line change
@@ -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 '),
})
6 changes: 6 additions & 0 deletions src/schemas/signInSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from "zod"

export const SignInSchema = z.object({
identifier: z.string(),
password:z.string()
})
14 changes: 14 additions & 0 deletions src/schemas/signUpSchema.ts
Original file line number Diff line number Diff line change
@@ -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"})
})

5 changes: 5 additions & 0 deletions src/schemas/verifySchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod"

export const verifySchema = z.object({
code:z.string().length(6,'verification code of 6 digits')
})

0 comments on commit d4e2549

Please sign in to comment.