Skip to content

Commit b98c772

Browse files
committed
backend initiated and user model done
1 parent 233188d commit b98c772

File tree

9 files changed

+1670
-0
lines changed

9 files changed

+1670
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
backend/node_modules
2+
backend/.env

backend/app.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from "express"
2+
import cookieParser from "cookie-parser"
3+
import cors from "cors"
4+
5+
const app = express()
6+
app.use(cors({
7+
origin: process.env.CORS_ORIGIN,
8+
credentials: true, //* credentials can be accessed over here
9+
}))
10+
app.use(express.json({ limit: "30kb" }))
11+
app.use(express.urlencoded({ extended: true, limit: "30kb" })) //* providing objects withing objects using url data transfer
12+
app.use(express.static("public")) //* using it to access static resources
13+
app.use(cookieParser()) //* cookies sent to the server via the browser are also parsed
14+
15+
16+
17+
18+
19+
20+
export { app }

backend/database/dbconnect.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import mongoose from "mongoose";
2+
export const connectDB = async () => {
3+
try {
4+
const connectionSuccess = await mongoose.connect(`${process.env.MONGODB_URL}`)
5+
//* it takes some time to connect hence await
6+
console.log(`DATABASE connected Successfully`)
7+
}
8+
catch (error) {
9+
// console.error("ERROR occured while connecting to database")
10+
console.error(error)
11+
12+
process.exit(1) //* to exit the process that is going on currently via node
13+
}
14+
}

backend/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import dotenv from "dotenv";
2+
import { connectDB } from "./database/dbconnect.js";
3+
import { app } from "./app.js";
4+
dotenv.config({
5+
path: './.env'
6+
});
7+
connectDB()
8+
.then(() => {
9+
app.listen(process.env.PORT, () => { console.log(`serving is running at ${process.env.PORT}`) })
10+
})
11+
.catch((error) => {
12+
console.log("MONGODB connection failed", error)
13+
})

backend/models/Assets.model.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import mongoose from "mongoose";
2+
import bcrypt from "bcryptjs";
3+
4+
const UserAssets = mongoose.Schema({
5+
user: {
6+
type: mongoose.Schema.Types.ObjectId,
7+
ref: "User",
8+
required: true,
9+
},
10+
monthlyincome: {
11+
type: Number,
12+
required: true,
13+
default: 0,
14+
},
15+
currentbankamount: {
16+
type: Number,
17+
required: true,
18+
default: 0,
19+
},
20+
currentsavings: {
21+
type: Number,
22+
required: true,
23+
default: 0,
24+
}
25+
})
26+
27+
UserAssets.pre("save", async function (next) {
28+
if (!this.isModified("monthlyincome")) return next();
29+
try {
30+
this.monthlyincome = await bcrypt.hash(this.monthlyincome, 10);
31+
next();
32+
}
33+
catch (error) {
34+
next(error)
35+
}
36+
})
37+
38+
UserAssets.pre("save", async function (next) {
39+
if (!this.isModified("currentbankamount")) return next();
40+
try {
41+
this.currentbankamount = await bcrypt.hash(this.currentbankamount, 10);
42+
next();
43+
}
44+
catch (error) {
45+
next(error)
46+
}
47+
})
48+
49+
UserAssets.pre("save", async function (next) {
50+
if (!this.isModified("currentsavings")) return next();
51+
try {
52+
this.currentsavings = await bcrypt.hash(this.currentsavings, 10);
53+
next();
54+
}
55+
catch (error) {
56+
next(error)
57+
}
58+
})
59+
60+
export const Assets = mongoose.model("Assets", UserAssets)
61+

backend/models/Liabilities.model.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mongoose from "mongoose";
2+
import bcrypt from "bcryptjs";
3+
4+
const UserLiabilities = mongoose.Schema({
5+
user: {
6+
type: mongoose.Schema.Types.ObjectId,
7+
ref: "User",
8+
required: true,
9+
},
10+
})
11+

backend/models/User.model.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import mongoose from "mongoose";
2+
import bcrypt from "bcryptjs"
3+
import jwt from "jsonwebtoken";
4+
5+
const UserSchema = mongoose.Schema(
6+
{
7+
username: {
8+
type: String,
9+
required: true,
10+
unique: true,
11+
index: true,
12+
trim: true
13+
},
14+
email: {
15+
type: String,
16+
required: true,
17+
unique: true,
18+
trim: true,
19+
lowercase: true
20+
},
21+
fullname: {
22+
type: String,
23+
required: true,
24+
lowercase: true,
25+
trim: true
26+
},
27+
coverimage: {
28+
type: String, //* url from cloudinary
29+
},
30+
31+
//* these consist of the id's of the blogs he read
32+
password: {
33+
type: String, //* because we will decrypt the password before storing it
34+
required: [true, "password is required"],
35+
36+
},
37+
familysize: {
38+
type: Number,
39+
default: 1,
40+
},
41+
contactno: {
42+
type: Number,
43+
},
44+
45+
46+
refreshToken: {
47+
type: String,
48+
}
49+
},
50+
{
51+
timestamps: true
52+
}
53+
)
54+
55+
UserSchema.pre("save", async function (next) {
56+
if (!this.isModified("password")) return next();
57+
try {
58+
this.password = await bcrypt.hash(this.password, 10);
59+
next();
60+
}
61+
catch (error) {
62+
next(error)
63+
}
64+
})
65+
UserSchema.methods.isPasswordCorrect = async function (password) {
66+
return await bcrypt.compare(password, this.password)
67+
}
68+
69+
UserSchema.methods.generateAccessToken = function () {
70+
return jwt.sign({
71+
_id: this._id,
72+
email: this.email,
73+
username: this.username,
74+
fullname: this.fullname
75+
},
76+
process.env.ACCESS_TOKEN_SECRET,
77+
{ expiresIn: process.env.ACCESS_TOKEN_EXPIRY }
78+
)
79+
}
80+
UserSchema.methods.generateRefreshToken = function () {
81+
return jwt.sign(
82+
{
83+
_id: this._id,
84+
},
85+
process.env.REFRESH_TOKEN_SECRET,
86+
{ expiresIn: process.env.REFRESH_TOKEN_EXPIRY }
87+
)
88+
}
89+
90+
export const User = mongoose.model("User", UserSchema)

0 commit comments

Comments
 (0)