Skip to content

Commit 820be15

Browse files
authored
Merge pull request #35 from acm-uic/creatingUser
create user function done
2 parents c1a740a + 178b7bc commit 820be15

File tree

10 files changed

+944
-13
lines changed

10 files changed

+944
-13
lines changed

backend/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
API_KEY =
2-
PORT =
1+
PORT =
2+
MONGODB =

backend/controllers/user.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import UserModel from "../models/user.js";
2+
import bcrypt from "bcrypt";
3+
4+
const UserControllers = {
5+
createUser: async (req, res) => {
6+
try {
7+
// Get the info from the user
8+
const { userName, email, password, avatar } = req.body;
9+
10+
// Check if there's any email alr existed
11+
const existedUser = await UserModel.findOne({
12+
email: email,
13+
});
14+
15+
// If alr exists an email,
16+
if (existedUser)
17+
throw new Error("Email existed! Please enter a different email.");
18+
// Create a hash password
19+
const saltRounds = 10;
20+
const salt = bcrypt.genSaltSync(saltRounds);
21+
const hashedPassword = bcrypt.hashSync(password, salt);
22+
23+
// Create a new user
24+
const newUser = await UserModel.create({
25+
userName,
26+
email,
27+
password: hashedPassword,
28+
avatar,
29+
});
30+
31+
res.status(201).send({
32+
message: "User created successfully",
33+
success: true,
34+
data: newUser,
35+
});
36+
} catch (error) {
37+
res.status(409).send({
38+
message: error.message,
39+
success: true,
40+
data: null,
41+
});
42+
}
43+
},
44+
};
45+
46+
export default UserControllers;

backend/database/collections.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Collections = {
2+
users: "users",
3+
};
4+
5+
export default Collections;

backend/index.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ import express from "express";
22
import cors from "cors";
33
import dotenv from "dotenv";
44
import http from "http";
5+
import mongoose from "mongoose";
6+
import { RootRouteV1 } from "./routes/index.js";
57

6-
7-
dotenv.config({path: ".env.local"});
8+
dotenv.config({ path: ".env.local" });
9+
await mongoose.connect(process.env.MONGODB);
810

911
const app = express();
1012
const server = http.createServer(app);
1113

1214
app.use(express.json());
1315
app.use(cors());
1416

15-
app.get("/", (req, res) => {
16-
res.status(200).send("Hello from the server")
17-
} )
18-
19-
server.listen(process.env.PORT, ()=> {
20-
console.log(`Server is running at port ${process.env.PORT}`)
21-
})
22-
17+
app.use("", RootRouteV1);
2318

24-
console.log(process.env.API_KEY)
19+
server.listen(process.env.PORT, () => {
20+
console.log(`Server is running at port ${process.env.PORT}`);
21+
});

backend/middlewares/user.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const UserMiddlewares = {
2+
// A middleware for the create user function
3+
createUser: (req, res, next) => {
4+
try {
5+
// Get the information from the user
6+
const { userName, email, password } = req.body;
7+
8+
// Give an error if missing any information
9+
if (!userName) throw new Error("Please enter username!");
10+
if (!email) throw new Error("Please enter email!");
11+
if (!password) throw new Error("Please enter password!");
12+
13+
return next();
14+
} catch (error) {
15+
res.status(400).send({
16+
message: error.message,
17+
success: false,
18+
data: null,
19+
});
20+
}
21+
},
22+
};
23+
24+
export default UserMiddlewares;

backend/models/user.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import mongoose from "mongoose";
2+
import Collections from "../database/collections.js";
3+
4+
// Create a schema for the user
5+
const UserSchema = mongoose.Schema({
6+
userName: {
7+
type: String,
8+
required: true,
9+
},
10+
email: {
11+
type: String,
12+
unique: true,
13+
required: true,
14+
},
15+
password: {
16+
type: String,
17+
required: true,
18+
},
19+
avatar: {
20+
type: String,
21+
default:
22+
"https://static.vecteezy.com/system/resources/thumbnails/009/292/244/small_2x/default-avatar-icon-of-social-media-user-vector.jpg",
23+
},
24+
});
25+
26+
// Creating a user model
27+
const UserModel = mongoose.model(Collections.users, UserSchema);
28+
29+
export default UserModel;

0 commit comments

Comments
 (0)