[[oktatas:web:nodejs:rest_api:sequelize|< Sequelize]]
====== Sequelize azonosítás ======
* **Szerző:** Sallai András
* Copyright (c) 2023, Sallai András
* Szerkesztve: 2023-2024
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== GitHub =====
* https://github.com/oktat/empjs.git
===== Kontroller =====
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const authconfig = require('../../config/auth.config.js');
const { sequelize, Sequelize } = require('../models');
const db = require('../models');
db.user = require('../models/user')(sequelize, Sequelize);
const User = db.user;
exports.register = (req, res) => {
console.log('-----')
if(!req.body.name) {
res.status(400).send({
message: "A név megadása kötelező!"
})
return
}
if(!req.body.email) {
res.status(400).send({
message: "Az email megadása kötelező!"
})
}
if(!req.body.password) {
res.status(400).send({
message: "A jelszó megadása kötelező!",
password: ""
})
}
if(!req.body.password_confirmation) {
res.status(400).send({
message: "A jelszó ismétlése kötelező",
password_confirmation: ""
})
}
if(req.body.password != req.body.password_confirmation) {
res.status(400).send({
message: "A jelszavak nem egyeznek!"
})
}
User.findOne({
where: {
name: req.body.name
}
})
.then(user => {
if(user) {
res.status(400).send({ message: "Already user " + user.name})
}else {
const user = {
name: req.body.name,
email: req.body.email,
password: bcrypt.hashSync(req.body.password)
}
User.create(user)
.then( result => {
res.status(201).send(result)
})
}
})
};
exports.login = (req, res) => {
console.log('-----')
if(!req.body.name) {
res.status(400).send({
message: "A név megadása kötelező!"
})
return
}
if(!req.body.password) {
res.status(400).send({
message: "A jelszó megadása kötelező!",
password: ""
})
}
const user = {
name: req.body.name,
password: bcrypt.hashSync(req.body.password)
}
User.findOne({
where: {
name: req.body.name
}
})
.then(user => {
if(!user) {
return res.status(404).send({ message: "User not found."})
}
var passwordIsValid = bcrypt.compareSync(
req.body.password,
user.password
);
if(!passwordIsValid) {
res.status(401).send({
accessToken: null,
message: "Invalid password!"
});
}
var token = jwt.sign({ id: user.id }, authconfig.secret, {
expiresIn: 86400 //24 óra
});
res.status(200).send({
id: user.id,
name: user.name,
email: user.email,
accessToken: token
});
})
};
===== Middleware =====
const jwt = require("jsonwebtoken");
const config = require("../../config/auth.config.js");
const db = require("../models");
const User = db.user;
exports.verifyToken = (req, res, next) => {
let authData = req.headers.authorization;
if(!authData) {
return res.status(403).send({
message: 'No token provided!'
})
}
let token = authData.split(' ')[1];
jwt.verify(token, config.secret, (err, decoded) => {
if(err) {
return res.status(401).send({
message: "Unauthorized!"
})
}
req.userId = decoded.id;
next()
})
};
===== Routing =====
module.exports = app => {
const router = require('express').Router();
const employees = require('../app/controllers/employee.controller');
const auth = require('../app/controllers/auth.controller');
const { verifyToken } = require('../app/middleware/authjwt');
router.get('/employees', employees.index);
router.post('/employees', [verifyToken], employees.store);
router.delete('/employees/:id', [verifyToken], employees.destroy);
router.put('/employees/:id', [verifyToken], employees.update);
router.post('/register', auth.register);
router.post('/login', auth.login);
app.use('/api', router);
}
===== Link =====
* https://www.bezkoder.com/node-js-jwt-authentication-mysql/ (2023)