Skip to content

Commit c1362a6

Browse files
committed
update modules to new versions
1 parent 7a96765 commit c1362a6

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ The diagram shows flow of how we implement User Registration, User Login and Aut
1414
For more detail, please visit:
1515
> [Node.js Express Login and Registration example with JWT](https://www.bezkoder.com/node-js-express-login-example/)
1616
17+
Front-end that works with this Node back-end:
18+
- [Angular 12](https://www.bezkoder.com/angular-12-jwt-auth-httponly-cookie/) / [Angular 13](https://www.bezkoder.com/angular-13-jwt-auth-httponly-cookie/) / [Angular 14](https://www.bezkoder.com/angular-14-jwt-auth/) / [Angular 15](https://www.bezkoder.com/angular-15-jwt-auth/) / [Angular 16](https://www.bezkoder.com/angular-16-jwt-auth/)
19+
- [React](https://www.bezkoder.com/react-login-example-jwt-hooks/) / [React Redux](https://www.bezkoder.com/redux-toolkit-auth/)
20+
1721
## More Practice:
1822
> [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://www.bezkoder.com/node-js-express-sequelize-mysql/)
1923
@@ -35,12 +39,10 @@ Associations:
3539
Deployment:
3640
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
3741
38-
Integration on same Server/Port:
39-
> [Integrate Angular 8 with Node.js Express](https://www.bezkoder.com/integrate-angular-8-node-js/)
40-
41-
> [Integrate Angular 10 with Node.js Express](https://www.bezkoder.com/integrate-angular-10-node-js/)
42+
> [Docker Compose: Node.js Express and MySQL example](https://www.bezkoder.com/docker-compose-nodejs-mysql/)
4243
43-
> [Integrate Angular 12 with Node.js Express](https://www.bezkoder.com/integrate-angular-12-node-js/)
44+
Integration on same Server/Port:
45+
> [Integrate Angular with Node.js Express](https://www.bezkoder.com/integrate-angular-12-node-js/)
4446
4547
> [Integrate Vue with Node.js Express](https://www.bezkoder.com/serve-vue-app-express/)
4648

app/controllers/auth.controller.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ exports.signin = async (req, res) => {
6161
});
6262
}
6363

64-
const token = jwt.sign({ id: user.id }, config.secret, {
65-
expiresIn: 86400, // 24 hours
66-
});
64+
const token = jwt.sign({ id: user.id },
65+
config.secret,
66+
{
67+
algorithm: 'HS256',
68+
allowInsecureKeySizes: true,
69+
expiresIn: 86400, // 24 hours
70+
});
6771

6872
let authorities = [];
6973
const roles = await user.getRoles();

app/middleware/authJwt.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ verifyToken = (req, res, next) => {
1212
});
1313
}
1414

15-
jwt.verify(token, config.secret, (err, decoded) => {
16-
if (err) {
17-
return res.status(401).send({
18-
message: "Unauthorized!",
19-
});
20-
}
21-
req.userId = decoded.id;
22-
next();
23-
});
15+
jwt.verify(token,
16+
config.secret,
17+
(err, decoded) => {
18+
if (err) {
19+
return res.status(401).send({
20+
message: "Unauthorized!",
21+
});
22+
}
23+
req.userId = decoded.id;
24+
next();
25+
});
2426
};
2527

2628
isAdmin = async (req, res, next) => {

app/models/index.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const sequelize = new Sequelize(
88
{
99
host: config.HOST,
1010
dialect: config.dialect,
11-
operatorsAliases: false,
12-
1311
pool: {
1412
max: config.pool.max,
1513
min: config.pool.min,
@@ -28,14 +26,10 @@ db.user = require("../models/user.model.js")(sequelize, Sequelize);
2826
db.role = require("../models/role.model.js")(sequelize, Sequelize);
2927

3028
db.role.belongsToMany(db.user, {
31-
through: "user_roles",
32-
foreignKey: "roleId",
33-
otherKey: "userId"
29+
through: "user_roles"
3430
});
3531
db.user.belongsToMany(db.role, {
36-
through: "user_roles",
37-
foreignKey: "userId",
38-
otherKey: "roleId"
32+
through: "user_roles"
3933
});
4034

4135
db.ROLES = ["user", "admin", "moderator"];

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"license": "ISC",
2222
"dependencies": {
2323
"bcryptjs": "^2.4.3",
24-
"cookie-session": "^1.4.0",
24+
"cookie-session": "^2.0.0",
2525
"cors": "^2.8.5",
26-
"express": "^4.17.1",
27-
"jsonwebtoken": "^8.5.1",
28-
"mysql2": "^2.1.0",
29-
"sequelize": "^6.11.0"
26+
"express": "^4.18.2",
27+
"jsonwebtoken": "^9.0.0",
28+
"mysql2": "^2.3.3",
29+
"sequelize": "^6.31.1"
3030
}
3131
}

server.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const cookieSession = require("cookie-session");
55
const app = express();
66

77
app.use(cors());
8+
/* for Angular Client (withCredentials) */
9+
// app.use(
10+
// cors({
11+
// credentials: true,
12+
// origin: ["http://localhost:8081"],
13+
// })
14+
// );
815

916
// parse requests of content-type - application/json
1017
app.use(express.json());
@@ -15,7 +22,7 @@ app.use(express.urlencoded({ extended: true }));
1522
app.use(
1623
cookieSession({
1724
name: "bezkoder-session",
18-
secret: "COOKIE_SECRET", // should use as secret environment variable
25+
keys: ["COOKIE_SECRET"], // should use as secret environment variable
1926
httpOnly: true,
2027
sameSite: 'strict'
2128
})

0 commit comments

Comments
 (0)