我的应用在JWT身份验证期间崩溃

时间:2019-06-22 14:00:42

标签: javascript node.js reactjs schema nodemon

我是MERN堆栈开发的新手,正在尝试JWT身份验证。在此期间,我面临着我的服务器无法正常工作的情况,它们每次都崩溃了。

昨天它在工作,但是今天它不工作。现在他们正在显示此错误。

This is error showing on console

  

MissingSchemaError:尚未为模型“用户”注册架构。

这是我的架构:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    avatar: {
        type: String
    },
    date: {
        type: Date,
        default: Date.now
    }
});

const User = mongoose.model('users', UserSchema);

module.exports = User;

此代码位于路线文件夹-user.js文件

// user.js

const express = require('express');
const router = express.Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const passport = require ('passport');
const validateRegisterInput = require('../validation/register');
const validateLoginInput = require('../validation/login');

const User = require('../models/User');


//Register Post Router
router.post('/register', function(req, res){
    const {errors, isValid} = validateRegisterInput(req.body);

    if(!isValid){
        return res.status(400).json(errors);
    }

    User.findOne({
        email: req.body.email
    }).then(user => {
        if(user){
            return res.status(400).json({
                email: 'Email already exists'
            });
        }
        else {
            const avatar = gravatar.url(req.body.email, {
                s: '200',
                r: 'pg',
                d: 'mm'
            });
            const newUser = new User({
                name: req.body.name,
                email: req.body.email,
                password: req.body.password,
                avatar
            });

            bcrypt.genSalt(10, (err, salt) => {
                if(err) 
                console.error('There was an error', err);
                else{
                    bcrypt.hash(newUser.password, salt, (err, hash) => {
                        if(err)
                        console.error('There was an error', err);
                        else{
                            newUser.password = hash;
                            newUser
                            .save()
                            .then(user => {
                                res.json(user)
                            });
                        }
                    });
                }
            });
        }
    });
});

//Login Post Router
router.post('/login', (req, ress) => {
    const {errors, isValid} = validateLoginInput(req.body);
    if(isValid){
        return res.status(400).json(errors);
    }
    const email = req.body.email;
    const password = req.body.password;

    User.findOne({email})
        .then(user => {
            if(!user) {
                errors.email = 'User not found'
                return res.status(404).json(errors);
            }
            bcrypt.compare(password, user.password)
                .then(isMatch => {
                    if(isMatch){
                        const payload = {
                            id: user.id,
                            name: user.name,
                            avatar: user.avatar
                        }
                        jwt.sign(payload, 'secret', {
                            expiresIn: 3600
                        }, (err, token) => {
                            if(err)
                            console.error('There is some error in token', err);
                            else{
                                res.json({
                                    success: true,
                                    token: `Bearer ${token}`
                                });
                            }
                        });
                    }
                    else{
                        errors.password = 'Incorrect Password';
                        return
                        res.status(400).json(errors);
                    }
                });
        });
});

router.get('/me', passport.authenticate('jwt', {session: false}), (req, res) => {
    return res.json({
        id: req.user.id,
        name: req.user.name,
        email: req.user.email
    });
});

module.exports = router;

0 个答案:

没有答案