Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion backend/controllers/api/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,53 @@ exports.createUser = async (request, response) => {
}
};

exports.login = async (request, response) => {
try {
// 1. Retrieve email and password
const { email, password } = request.body;

// Assuming email or password was not provided
if (!email || !password) {
throw new Error("Email and password are required fields");
}

// 2. Use the email to find user
const currentUser = await User.findOne({ email }).select("+password");

// 3. Create an instance method to compare password
if (
!currentUser ||
!(await currentUser.comparePassword(password, currentUser.password))
) {
throw new Error("Invalid email or password");
}

// 4. If the code reach this point it means that the password was correct
const token = jwt.sign(
{ id: currentUser._id },
process.env.JWT_SECRET_KEY,
{ expiresIn: process.env.JWT_EXPIRATION_DATE }
);

// 5. Remove password from output
currentUser.password = undefined;

// 6. Send response
response.status(200).json({
status: "success",
data: {
currentUser,
token,
},
});
} catch (error) {
response.status(400).json({
status: "fail",
message: error.message,
});
}
};

// Define a route handler for retrieving the a single user
exports.getUser = async (request, response) => {
try {
Expand All @@ -65,4 +112,4 @@ exports.getUser = async (request, response) => {
message: error.message,
});
}
};
};
8 changes: 8 additions & 0 deletions backend/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ userSchema.pre("save", async function (next) {
next();
});

// Create an instance method to compare password using bcryptjs
userSchema.methods.comparePassword = async function (
plainText,
hashedPassword
) {
return await bcrypt.compare(plainText, hashedPassword);
};

// Use mongoose and schema to create user model
const User = mongoose.model("User", userSchema);

Expand Down
1 change: 1 addition & 0 deletions backend/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const router = express.Router();

// Use the router to redirect to different controller depending on the method
router.route("/").post(userController.createUser);
router.route("/login").post(userController.login);
router.route("/:id").get(userController.getUser);

// EXPORT ROUTER TO BE USED IN OTHER PARTS OF OUR APPLICATION
Expand Down
28 changes: 17 additions & 11 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
const path = require("path")
const express = require("express")
const morgan = require("morgan")
const favicon = require("serve-favicon")
const dotenv = require("dotenv")
const mongoose = require("mongoose")
const path = require("path");
const express = require("express");
const morgan = require("morgan");
const favicon = require("serve-favicon");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const cors = require("cors");

const userRouter = require("./routes/api/users");

dotenv.config()
dotenv.config();

const DB = mongoose.connect(process.env.DATABASE, { useNewUrlParser: true, useUnifiedTopology: true })
const DB = mongoose
.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("DB CONNECTION SUCCESSFUL!");
});

const app = express();
const app = express();

//middleware
app.use(cors());

app.use(morgan("dev"));

Expand All @@ -31,5 +37,5 @@ app.all("*", (request, response) => {

const port = 3001;
app.listen(port, () => {
console.log(`server started on port ${port}. . .`)
})
console.log(`server started on port ${port}. . .`);
});
24 changes: 24 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
node_modules/
# testing
/coverage

# production
/build

# misc
.env
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading