Я вхожу в систему с настоящим тестовым пользователем, которого я вижу в БД, но ничего не происходит, и я остаюсь на странице входа. Я не вижу ошибок нигде, и я могу перейти к другим компонентам, введя их URL. Пожалуйста, дайте мне знать, если какой-либо дополнительный код поможет.
user.service. js
const config = require('config.json');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const db = require('../DB');
const User = db.User;
module.exports = {
authenticate,
getAll,
getById,
create,
update,
delete: _delete
};
async function authenticate({ username, password }) {
const user = await User.findOne({ username });
if (user && bcrypt.compareSync(password, user.hash)) {
const { hash, ...userWithoutHash } = user.toObject();
const token = jwt.sign({ sub: user.id }, config.secret);
return {
...userWithoutHash,
token
};
}
}
async function getAll() {
return await User.find().select('-hash');
}
async function getById(id) {
return await User.findById(id).select('-hash');
}
async function create(userParam) {
// validate
if (await User.findOne({ username: userParam.username })) {
throw 'Username "' + userParam.username + '" is already taken';
}
const user = new User(userParam);
// hash password
if (userParam.password) {
user.hash = bcrypt.hashSync(userParam.password, 10);
}
// save user
await user.save();
}
async function update(id, userParam) {
const user = await User.findById(id);
// validate
if (!user) throw 'User not found';
if (user.username !== userParam.username && await User.findOne({ username: userParam.username })) {
throw 'Username "' + userParam.username + '" is already taken';
}
// hash password if it was entered
if (userParam.password) {
userParam.hash = bcrypt.hashSync(userParam.password, 10);
}
// copy userParam properties to user
Object.assign(user, userParam);
await user.save();
}
async function _delete(id) {
await User.findByIdAndRemove(id);
}
user.controller. js
const express = require('express');
const router = express.Router();
const userService = require('./user.service');
// routes
router.post('/authenticate', authenticate);
router.post('/register', register);
router.get('/', getAll);
router.get('/current', getCurrent);
router.get('/:id', getById);
router.put('/:id', update);
router.delete('/:id', _delete);
module.exports = router;
function authenticate(req, res, next) {
userService.authenticate(req.body)
.then(user => user ? res.json(user) : res.status(400).json({ message: 'Username or password is incorrect' }))
.catch(err => next(err));
}
function register(req, res, next) {
userService.create(req.body)
.then(() => res.json({}))
.catch(err => next(err));
}
function getAll(req, res, next) {
userService.getAll()
.then(users => res.json(users))
.catch(err => next(err));
}
function getCurrent(req, res, next) {
userService.getById(req.user.sub)
.then(user => user ? res.json(user) : res.sendStatus(404))
.catch(err => next(err));
}
function getById(req, res, next) {
userService.getById(req.params.id)
.then(user => user ? res.json(user) : res.sendStatus(404))
.catch(err => next(err));
}
function update(req, res, next) {
userService.update(req.params.id, req.body)
.then(() => res.json({}))
.catch(err => next(err));
}
function _delete(req, res, next) {
userService.delete(req.params.id)
.then(() => res.json({}))
.catch(err => next(err));
}
user .route. js
const express = require('express');
const router = express.Router();
const userRoutes = express.Router();
const User = require('../models/User');
// routes
router.post('/authenticate', authenticate);
router.post('/register', register);
router.get('/', getAll);
router.get('/current', getCurrent);
router.get('/:id', getById);
router.put('/:id', update);
router.delete('/:id', _delete);
module.exports = router;
function authenticate(req, res, next) {
userService.authenticate(req.body)
.then(user => user ? res.json(user) : res.status(400).json({ message: 'Username or password is incorrect' }))
.catch(err => next(err));
}
function register(req, res, next) {
userService.create(req.body)
.then(() => res.json({}))
.catch(err => next(err));
}
function getAll(req, res, next) {
userService.getAll()
.then(users => res.json(users))
.catch(err => next(err));
}
function getCurrent(req, res, next) {
userService.getById(req.user.sub)
.then(user => user ? res.json(user) : res.sendStatus(404))
.catch(err => next(err));
}
function getById(req, res, next) {
userService.getById(req.params.id)
.then(user => user ? res.json(user) : res.sendStatus(404))
.catch(err => next(err));
}
function update(req, res, next) {
userService.update(req.params.id, req.body)
.then(() => res.json({}))
.catch(err => next(err));
}
function _delete(req, res, next) {
userService.delete(req.params.id)
.then(() => res.json({}))
.catch(err => next(err));
}