<commit_before>var bcrypt = require('bcryptjs');

module.exports = {
    /**
     * Controller action called when authenticating a user
     */
    auth: function (req, res) {
        var findCriteria = {}

        //Checks if email/username and password is found
        if (sails.config.jsonWebToken.authType == "email") {
            // Validate request paramaters. If email and password exist
            if (!req.body.email || !req.body.password) {
                return res.json(401, {
                    err: {
                        status: 'danger',
                        message: 'Email or password not found'
                    }
                });
            }

            findCriteria["email"] = req.body.email
        } else {
            // Validate request paramaters. If username and password exist
            if (!req.body.username || !req.body.password) {
                return res.json(401, {
                    err: {
                        status: 'danger',
                        message: 'Username or password not found'
                    }
                });
            }

            findCriteria["username"] = req.body.username
        }

        //find email matching user
        User.findOne(findCriteria).then((user) => {
            if (!user) {
                return res.json(401, {
                    err: {
                        status: 'warn',
                        message: 'User not found'
                    }
                });
            }

            //validate password (password, callback)
            user.isPasswordValid(req.body.password, function (err, match) {
                if (err) return res.serverError(err);
                if (match) {//issue a token to the user
                    JwtService.issueToken({ user_id: user.id, user_accountType: user.accountType }, user).then((token) => {
                        //call afterSignin function
                        sails.config.jsonWebToken.afterSignin(user)
                        return res.json({ user: user, token: token });
                    }).catch((err) => {
                        return res.serverError(err);
                    })

                } else {
                    return res.json(401, {
                        err: {
                            status: 'danger',
                            message: 'Invalid password'
                        }
                    });
                }
            });
        }).catch((err) => {
            return res.serverError(err);
        });
    },


    /**
     * Controller action called when signing up
     */
    signup: function (req, res) {
        JwtService.createUser(req.body).then(user => {
            JwtService.issueToken({ user_id: user.id, user_accountType: user.accountType }, user).then((token) => {
                //after signup function
                sails.config.jsonWebToken.afterSignup(user)
                return res.json({ user: user, token: token });
            }).catch((err) => {
                return res.json(401, {
                    err: {
                        status: 'danger',
                        message: 'Unable to create token for new account !!, account created'
                    },
                    message: err
                });
            });
        }).catch(err => {
            return res.json(400, {
                err: {
                    status: 'danger',
                    message: 'Unable to create account'
                },
                message: err
            });
        })
    }
};<commit_msg>updated policy to use sails formal response<commit_after>var bcrypt = require('bcryptjs');

module.exports = {
    /**
     * Controller action called when authenticating a user
     */
    auth: function (req, res) {
        var findCriteria = {}

        //Checks if email/username and password is found
        if (sails.config.jsonWebToken.authType == "email") {
            // Validate request paramaters. If email and password exist
            if (!req.body.email || !req.body.password) {
                return res.badRequest('Email or password not found')
            }

            findCriteria["email"] = req.body.email
        } else {
            // Validate request paramaters. If username and password exist
            if (!req.body.username || !req.body.password) {
                return res.badRequest('Username or password not found')
            }

            findCriteria["username"] = req.body.username
        }

        //find email matching user
        User.findOne(findCriteria).then((user) => {
            if (!user) {
                return res.badRequest('User not found')
            }

            //validate password (password, callback)
            user.isPasswordValid(req.body.password, function (err, match) {
                if (err) return res.negotiate(err);
                if (match) {//issue a token to the user
                    JwtService.issueToken({ user_id: user.id, user_accountType: user.accountType }, user).then((token) => {
                        //call afterSignin function
                        sails.config.jsonWebToken.afterSignin(user)
                        return res.json({ user: user, token: token });
                    }).catch((err) => {
                        return res.negotiate(err);
                    })

                } else {
                    return res.badRequest('Invalid password')
                }
            });
        }).catch((err) => {
            return res.negotiate(err);
        });
    },


    /**
     * Controller action called when signing up
     */
    signup: function (req, res) {
        JwtService.createUser(req.body).then(user => {
            JwtService.issueToken({ user_id: user.id, user_accountType: user.accountType }, user).then((token) => {
                //after signup function
                sails.config.jsonWebToken.afterSignup(user)
                return res.json({ user: user, token: token });
            }).catch((err) => {
                return res.negotiate(err)
            });
        }).catch(err => {
            return res.negotiate(err)
        })
    }
};
{'fraction_non_alphanumeric': 0.11199095022624435, 'fraction_numerical': 0.0029088558500323207, 'mean_word_length': 1.2448313384113165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7535183', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1461, 'n_words': 363}