Skip to content

Commit 411da75

Browse files
committed
Working on auth for api
1 parent 36cd9c3 commit 411da75

27 files changed

+15118
-0
lines changed

api/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "api",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "nodemon ./src/index.js --watch ./src",
8+
"test": "nyc mocha ./test/**/*.test.js --require=./test/test-helper.js --timeout=20000"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"bcrypt": "^3.0.6",
15+
"cors": "^2.8.5",
16+
"express": "^4.17.1",
17+
"express-async-router": "^0.1.15",
18+
"express-validator": "^6.2.0",
19+
"jsonwebtoken": "^8.5.1",
20+
"mongoose": "^5.7.3",
21+
"morgan": "^1.9.1"
22+
},
23+
"devDependencies": {
24+
"chai": "^4.2.0",
25+
"chai-http": "^4.3.0",
26+
"mocha": "^6.2.1",
27+
"nodemon": "^1.19.3",
28+
"nyc": "^14.1.1"
29+
}
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { AsyncRouter } = require("express-async-router");
2+
const jwt = require("jsonwebtoken");
3+
const { check, validationResult } = require("express-validator");
4+
5+
const User = require("../models/User");
6+
const handleValidationErrors = require("../helpers/handleValidationErrors");
7+
8+
const router = AsyncRouter();
9+
10+
const signUpValidators = [
11+
check("email").isEmail(),
12+
check("password").exists(),
13+
check("passwordConfirm").exists()
14+
];
15+
16+
const loginValidators = [
17+
check("email").isEmail(),
18+
check("password").exists()
19+
];
20+
21+
router.post(
22+
"/sign-up",
23+
[...signUpValidators, handleValidationErrors],
24+
async (req, res) => {
25+
const userExists = await User.findOne({email: req.body.email});
26+
27+
if(userExists)
28+
return res.status(400).send("E-mail already exists");
29+
if(req.body.password !== req.body.passwordConfirm)
30+
return res.status(400).send("Passwords do not match");
31+
32+
const user = await User.signUp(req.body.email, req.body.password);
33+
res.status(201).send(user);
34+
}
35+
);
36+
37+
router.post(
38+
"/login",
39+
[...loginValidators, handleValidationErrors],
40+
async (req, res) => {
41+
42+
}
43+
);
44+
45+
module.exports = router;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { validationResult } = require("express-validator");
2+
3+
const handleValidationErrors = (req, res, next) => {
4+
const errors = validationResult(req);
5+
if (!errors.isEmpty()) {
6+
return res.status(422).json({ errors: errors.array() });
7+
}
8+
9+
next();
10+
}
11+
12+
module.exports = handleValidationErrors;

api/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { startServer } = require("./server");
2+
3+
startServer();

api/src/models/User.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const mongoose = require("mongoose");
2+
const bcrypt = require("bcrypt");
3+
4+
const { Schema } = mongoose;
5+
6+
const userSchema = Schema({
7+
email: {
8+
type: String,
9+
required: true,
10+
unique: true,
11+
},
12+
password: {
13+
type: String,
14+
required: true,
15+
},
16+
}, {
17+
timestamps: true,
18+
toJSON: {
19+
virtuals: true,
20+
},
21+
toObject: {
22+
virtuals: true,
23+
}
24+
});
25+
26+
userSchema.statics.signUp = async function(email, password) {
27+
const user = new this();
28+
user.email = email;
29+
user.hashPassword(password);
30+
31+
await user.save();
32+
33+
return user;
34+
};
35+
userSchema.methods.hashPassword = function(plainText) {
36+
this.password = bcrypt.hashSync(plainText, 4);
37+
};
38+
39+
const User = mongoose.model("User", userSchema);
40+
41+
module.exports = User;

api/src/server.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const express = require("express");
2+
const mongoose = require("mongoose");
3+
const morgan = require("morgan");
4+
const cors = require("cors");
5+
6+
const AuthController = require("./controllers/auth.controller");
7+
8+
const app = express();
9+
app.use(cors());
10+
app.use(morgan("tiny"));
11+
app.use(express.json());
12+
13+
app.use("/auth", AuthController);
14+
15+
const connectDatabase = async (databaseName="fullstackjs-boilerplate", hostname="localhost") => {
16+
const database = await mongoose.connect(
17+
`mongodb://${hostname}/${databaseName}`,
18+
{
19+
useNewUrlParser: true,
20+
useUnifiedTopology: true,
21+
useCreateIndex: true
22+
}
23+
);
24+
25+
if(process.env.ENV !== "test")
26+
console.log(`🗑️ Database connected at mongodb://${hostname}/${databaseName}...`)
27+
28+
return database;
29+
}
30+
31+
const startServer = (hostname="localhost", port=1337) => {
32+
app.listen(port, hostname, async () => {
33+
await connectDatabase();
34+
if(process.env.ENV !== "test")
35+
console.log(`🚀 Server started at ${hostname}:${port}...`)
36+
});
37+
}
38+
39+
module.exports = {
40+
app,
41+
connectDatabase,
42+
startServer
43+
};

api/test/test-helper.js

Whitespace-only changes.

0 commit comments

Comments
 (0)