<commit_before>
var jwt = require('jsonwebtoken');
var appConfig = require('../config/appConfig');
var User = require('../models/User');

module.exports = {
    signup: function(req, res) {
        var username = req.body.username;
        var password = req.body.password;
        if(!username) {
            return res.json({
                success: false,
                message: "用户名不能为空"
            });
        }
        if(!password) {
            return res.json({
                success: false,
                message: "密码不能为空"
            });
        }
        if(password.length < 6) {
            return res.json({
                success: false,
                message: "密码至少6位"
            });
        }
        var user = new User();
        user.username = username;
        user.password = password;
        
        user.save(function(err, insertedUser) {
            if(err) {
                if(err.code == 11000 && err.name == "MongoError") {
                    res.status(400).send({success: false, message: "该用户已存在"});
                } else{
                    throw err;
                }
            }else{
                jwt.sign({id: insertedUser.id, user: {username: insertedUser.username, date: insertedUser.date, admin: insertedUser.admin}}, appConfig.secret, {expiresIn: appConfig.expireTime}, function(error, token) {
                    if(!error) { 
                        res.json({success: true, message: "注册成功", token: token});
                    }else{
                        throw error;
                    }
                });
            }
        });
    },

    login: function (req, res) {
        var username = req.body.username;
        var password = req.body.password;

        if(!username) {
            return res.json({
                success: false,
                message: "用户名不能为空"
            });
        }
        if(!password) {
            return res.json({
                success: false,
                message: "密码不能为空"
            });
        }

        User.findOne({username: username}, function (err, user) {
            if(!err) {
                user.comparePassword(password, function (err, isMatch) {
                    if(!isMatch) {
                        return res.json({
                            success: false,
                            message: "密码错误"
                        })
                    } else{
                        jwt.sign({id: user.id, user: {username: user.username, date: user.date, admin: user.admin}}, appConfig.secret, {expiresIn: appConfig.expireTime}, function(error, token) {
                            if(!error) { 
                                return res.json({
                                    success: true,
                                    message: "给你token",
                                    token: token
                                });
                            }else{
                                throw error;
                            }
                        });
                        
                    }
                })
            }else{
                return res.json({
                    success: false,
                    message: "用户不存在"<commit_msg>登录逻辑加强判断<commit_after>
var jwt = require('jsonwebtoken');
var appConfig = require('../config/appConfig');
var User = require('../models/User');

module.exports = {
    signup: function(req, res) {
        var username = req.body.username;
        var password = <PASSWORD>;
        if(!username) {
            return res.json({
                success: false,
                message: "用户名不能为空"
            });
        }
        if(!password) {
            return res.json({
                success: false,
                message: "密码不能为空"
            });
        }
        if(password.length < 6) {
            return res.json({
                success: false,
                message: "密码至少6位"
            });
        }
        var user = new User();
        user.username = username;
        user.password = password;
        
        user.save(function(err, insertedUser) {
            if(err) {
                if(err.code == 11000 && err.name == "MongoError") {
                    res.status(400).send({success: false, message: "该用户已存在"});
                } else{
                    throw err;
                }
            }else{
                jwt.sign({id: insertedUser.id, user: {username: insertedUser.username, date: insertedUser.date, admin: insertedUser.admin}}, appConfig.secret, {expiresIn: appConfig.expireTime}, function(error, token) {
                    if(!error) { 
                        res.json({success: true, message: "注册成功", token: token});
                    }else{
                        throw error;
                    }
                });
            }
        });
    },

    login: function (req, res) {
        var username = req.body.username;
        var password = req.body.password;

        if(!username) {
            return res.json({
                success: false,
                message: "用户名不能为空"
            });
        }
        if(!password) {
            return res.json({
                success: false,
                message: "密码不能为空"
            });
        }

        User.findOne({username: username}, function (err, user) {

            if(!err && user) {
        
                user.comparePassword(password, function (err, isMatch) {
                    if(!isMatch) {
                        return res.json({
                            success: false,
                            message: "密码错误"
                        })
                    } else{
                        jwt.sign({id: user.id, user: {username: user.username, date: user.date, admin: user.admin}}, appConfig.secret, {expiresIn: appConfig.expireTime}, function(error, token) {
                            if(!error) { 
                                return res.json({
                                    success: true,
                                    message: "给你token",
                                    token: token
                                });
                            }else{
                                throw error;
                            }
                        });
                        
                    }
                })
            }else{
                return res.json({
                    success: false,
                    message: "用户不存在"
{'fraction_non_alphanumeric': 0.11014851485148515, 'fraction_numerical': 0.003094059405940594, 'mean_word_length': 0.9656430525995744, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '570128', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1598, 'n_words': 328}