Skip to content

Commit

Permalink
update register route & controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Duffigoogle committed Jun 22, 2023
1 parent 985be60 commit 85c670b
Show file tree
Hide file tree
Showing 7 changed files with 865 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
.env
12 changes: 9 additions & 3 deletions controller/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bcrypt = require("bcryptjs");
const salt = 10

// User registration controller
const registerUser = async(req, res) => {
const registerUser = async(req, res, next) => {
const {firstname, username, email, password} = req.body;

if (!firstname || !username || !email || !password) {
Expand All @@ -30,7 +30,8 @@ const registerUser = async(req, res) => {
email: email,
password: bcrypt.hashSync(password, salt)
})
res.status(200).json({newUser});
res.status(200).json({message: "User signed in successfully", success: true, newUser});
next();
} catch (error) {
res.status(500).json({error: error.message})
}
Expand All @@ -52,4 +53,9 @@ module.exports = {
registerUser,
loginUser,
signoutUser
}
}

// The user's inputs are obtained from the req.body in the code above, and you then check the email to make sure no past registrations have been made. We'll use the values obtained from req.body to create the new user after that has occurred.


// You don't need to worry about how the unique _id was obtained because MongoDB always assigns a new user with a unique _id.
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const pageRoutes = require("./routes/pageRoutes")
const apiRoutes = require("./routes/apiRoutes")
const authRoutes = require("./routes/authRoutes")
const mongoose = require("mongoose")
const cors = require("cors");
require("dotenv").config();
const port = 6000

Expand All @@ -17,12 +18,19 @@ const port = 6000
app.use(express.urlencoded({extended: true}));
app.use(express.json())

app.use(
cors({
origin: ["http://localhost:6000"],
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
})
);

//ROUTES & ENDPOINTS
// Is a function that gets executed between when a request is made, and when a response is returned.
app.use("/", pageRoutes);
app.use("/api/v1/notes", apiRoutes);
app.use("auth/", authRoutes);
app.use("/auth", authRoutes);

// Connecting to MongoDB Database
mongoose.connect(process.env.MONGODB_URL, {
Expand All @@ -44,4 +52,15 @@ app.listen(port, () => {

// the event handler function accepts two parameters.
// The first request parameter contains all of the information of the HTTP request.
// The second response parameter is used to define how the request is responded to.
// The second response parameter is used to define how the request is responded to.


// CORS (Cross origin resource sharing): You can allow requests from other domains to access the resources on your server by using the cors() express middleware function. The CORS headers that your server should include in the response can be specified using the function's optional configuration object parameter, which is taken as a parameter by the function which is the origin, methods and credentials.


// express.json(): The express.json() will add a body property to the request or req object. This includes the request body's parsed JSON data. req.body in your route handler function will allow you to access this data.

// useNewUrlParser: This property specifies that Mongoose should use the new URL parser to parse MongoDB connection strings. This is set to true by default.


// useUnifiedTopology: This property specifies that Mongoose should use the new Server Discovery and Monitoring engine. This is set to false by default.
12 changes: 8 additions & 4 deletions models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ const mongoose = require("mongoose");


const userSchema = mongoose.Schema({
firstname: {type: String, required: true, trim: true},
username: {type: String, required: true, unique: true, trim: true, maxLength: 8},
email: {type: String, required: true, unique: true, trim: true},
password: {type: String, required: true, trim: true}
firstname: {type: String, required: [true, "Your firstname is required"], trim: true},
username: {type: String, required: [true, "Your username is required"], unique: true, trim: true, maxLength: 8},
email: {type: String, required: [true, "Your email address is required"], unique: true, trim: true},
password: {type: String, required: [true, "Your password is required"], trim: true},
createdAt: {
type: Date,
default: new Date(),
}
}, {
timestamps: true
});
Expand Down
51 changes: 26 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
{
"name": "expressserver",
"version": "1.0.0",
"description": "A simple express server\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[Dweb \u001b[C\u001bserver",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"express",
"web",
"server"
],
"author": "Caleb Duff",
"license": "ISC",
"dependencies": {
"dotenv": "^16.2.0",
"express": "^4.18.2",
"mongoose": "^7.3.0"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
"name": "expressserver",
"version": "1.0.0",
"description": "A simple express server\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[Dweb \u001b[C\u001bserver",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"express",
"web",
"server"
],
"author": "Caleb Duff",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.2.0",
"express": "^4.18.2",
"mongoose": "^7.3.0"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
2 changes: 1 addition & 1 deletion routes/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {updateNote,
removeNote,
getAllNotes,
getSingleNote,
addNewNote} = require("../controller/apiController")
addNewNote} = require("../controller/apiControllers")


// API Route OR Resource endpoints
Expand Down
Loading

0 comments on commit 85c670b

Please sign in to comment.