Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amarell dev #3

Merged
merged 4 commits into from
Apr 20, 2022
Merged
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
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const forExport = {
DB_OPTIONS,
};

export default forExport;
module.exports = forExport;
67 changes: 67 additions & 0 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const e = require("express");
const { entityFactory } = require("../entityFactory");
const User = require("../models/userModel");
const { registerValidation, loginValidation } = require("./../validation");
const jwt = require("./../utilities/jwt");

module.exports.register = async (req, res) => {
const errors = registerValidation(req.body);
if (errors.length > 0) {
return res.status(400).json({
errors,
});
}

const emailExists = await User.findOne({ email: req.body.email });
if (emailExists) {
return res.status(400).json({
error: "This email is already in use.",
});
}

const user = new User();
entityFactory(user, req.body);

await user.save((err, savedUser) => {
if (err) {
return res.status(400).json({
status: "Something went wrong",
});
} else {
return res.status(200).json({
data: savedUser,
});
}
});
};

module.exports.login = async (req, res) => {
const errors = loginValidation(req.body);

if (errors.length > 0) {
return res.status(400).json({
errors,
});
}

const user = await User.findOne({ email: req.body.email });

if (!user) {
return res.status(400).json({
error: "The user with this email address does not exist",
});
}

if (user.password !== req.body.password) {
return res.status(400).json({
error: "Email or password is incorrect",
});
}

let token = jwt.sign(user);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf, gdje ti je secret

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha ovo je custom nvm


return res.status(200).json({
success: true,
token,
});
};
7 changes: 7 additions & 0 deletions entityFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const entityFactory = (entity, requestBody) => {
Object.keys(requestBody).forEach((key) => {
entity[key] = requestBody[key];
});
};

module.exports.entityFactory = entityFactory;
185 changes: 185 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.0.0",
"description": "Online tech shop backend",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -26,6 +25,7 @@
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.5.0",
"mongoose": "^6.3.0",
"nodemon": "^2.0.15"
Expand Down
14 changes: 14 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const router = require("express").Router();
let userController = require("../controllers/userController");

router.get("/", (req, res) => {
res.json({
message: "API is working",
version: "1.0.0",
});
});

router.route("/register").post(userController.register);
router.route("/login").post(userController.login);

module.exports = router;
18 changes: 10 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express, { urlencoded, json } from "express";
import mongoose from "mongoose";
let express = require("express");
let mongoose = require("mongoose");
let port = process.env.PORT || 3000;
import config from "./config.js";
import cors from "cors";
import dotenv from "dotenv";
let config = require("./config.js");
let cors = require("cors");
require("dotenv").config();

dotenv.config();
// Import routes here
let authRoutes = require("./routes/auth");

const app = express();

Expand All @@ -16,15 +17,16 @@ app.listen(port, () => {
});

app.use(
urlencoded({
express.urlencoded({
extended: true,
})
);

app.use(json());
app.use(express.json());

// Here we will add all the routes!
// app.use("/api", userRoute)
app.use("/", authRoutes);

const mongo = mongoose.connect(process.env.DB_PATH, config.DB_OPTIONS);

Expand Down
Loading