Eric Whale

Add signin, signup functionality

const jwt = require("jsonwebtoken");
const bcrypt = require("bcryptjs");
// handles "exception" inside of async express routes
// (Used for mongoDB error in this project)
const asyncHandler = require("express-async-handler");
const User = require("../models/userModel");
// @desc Signup new user
// @route POST /api/users
// @access Public
const signupUser = asyncHandler(async (req, res) => {
const { username, email, password } = req.body;
if (!name || !email || !password) {
if (!username || !email || !password) {
res.status(400);
throw new Error("Please fill in all fields");
}
// Check if user already exists
const userExists = await User.findOne({ email });
if (userExists) {
res.status(400);
throw new Error("User with the email already exists");
}
// Hash password (bcrypt)
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
// Create/Build user
const user = await User.create({
username,
email,
password: hashedPassword,
});
// Send response
if (user) {
// 201: Resource successfully created
res.status(201).json({
_id: user.id,
username: user.username,
email: user.email,
// TODO: Add token!
});
} else {
res.status(400);
throw new Error("Invalid user data");
}
});
// @desc Login user
// @route POST /api/users/login
// @access Public
const loginUser = asyncHandler(async (req, res) => {
const { email, password } = req.body;
// Check for the user email
// Send response
// Check email & password
const userInDB = await User.findOne({ email });
const validPassword = await bcrypt.compare(password, userInDB.password);
if (userInDB && validPassword) {
res.status(200).json({
_id: userInDB.id,
username: userInDB.username,
email: userInDB.email,
// TODO: Add Token!
});
} else {
res.status(400);
throw new Error("Invalid credentials");
}
});
// @desc Get user(only self)
// @route GET /api/users/self
// @access Private
const getSelf = asyncHandler(async (req, res) => {});
module.exports = {
......
const jwt = require("jsonwebtoken");
const authHandler = (err, req, res, next) => {
next();
};
module.exports = { authHandler };
......@@ -4,7 +4,7 @@ const errorHandler = (err, req, res, next) => {
res.status(statusCode);
res.json({
message: err.message,
// stack from mongoDB (maybe...)
// stack from mongoDB TODO: Check it!
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};
......
......@@ -10,6 +10,7 @@
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"colors": "^1.4.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-async-handler": "^1.2.0",
......@@ -457,6 +458,14 @@
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==",
"engines": {
"node": ">=0.1.90"
}
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
......@@ -2591,6 +2600,11 @@
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
......
......@@ -5,7 +5,7 @@
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
"server": "nodemon server.js"
},
"repository": {
"type": "git",
......@@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"colors": "^1.4.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-async-handler": "^1.2.0",
......
const express = require("express");
const router = express.Router();
const { signupUser, loginUser } = require("../actions/userActions");
const { signupUser, loginUser, getSelf } = require("../actions/userActions");
router.post("/", signupUser);
router.post("/login", loginUser);
router.get("/self", getSelf);
module.exports = router;
......
......@@ -2,7 +2,7 @@ const express = require("express");
const dotenv = require("dotenv").config();
const { errorHandler } = require("./middleware/errorMiddleware");
const connectDB = require("./config/db");
const port = process.env.PORT || 8000;
const port = process.env.PORT || 6000;
connectDB();
const app = express();
......