<commit_before>/**
 * AuthController
 * @description :: Server-side logic for manage user's authorization
 */

var passport = require('passport');

module.exports = {
    /**
     * Sign in by local strategy in passport
     */
    signin: function (req, res) {
        passport.authenticate('local', function (error, user, info) {
            if (error) {
                sails.log.error(error);
                res.serverError(error);
            } else if (info) {
                res.unauthorized(null, info.code, info.message);
            } else {
                res.ok({
                    token: CipherService.create('jwt', {id: user.id}).hashSync(),
                    user: user
                });
            }
        })(req, res);
    },

    /**
     * Sign up in system
     */
    signup: function (req, res) {
        // TODO: think about model duplicate

        User
            .create(req.allParams())
            .exec(function (error, user) {
                if (error) {
                    sails.log.error(error);
                    res.serverError(error);
                } else {
                    res.ok({
                        token: CipherService.create('jwt', {id: user.id}).hashSync(),
                        user: user
                    });
                }
            });
    },

    /**
     * Facebook authorization\linking
     */
    facebook: function (req, res) {
        passport.authenticate('jwt', function (error, user) {
            req.user = user;

            passport.authenticate('facebook-token', function (error, user, info) {
                if (error) {
                    sails.log.error(error);
                    res.serverError(error);
                } else if (info) {
                    res.unauthorized(null, info.code, info.message);
                } else {
                    res.ok({
                        token: CipherService.create('jwt', {id: user.id}).hashSync(),
                        user: user
                    });
                }
            })(req, res);
        })(req, res);
    },

    /**<commit_msg>Replace ok with created response<commit_after>/**
 * AuthController
 * @description :: Server-side logic for manage user's authorization
 */

var passport = require('passport');

module.exports = {
    /**
     * Sign in by local strategy in passport
     */
    signin: function (req, res) {
        passport.authenticate('local', function (error, user, info) {
            if (error) {
                sails.log.error(error);
                res.serverError(error);
            } else if (info) {
                res.unauthorized(null, info.code, info.message);
            } else {
                res.ok({
                    token: CipherService.create('jwt', {id: user.id}).hashSync(),
                    user: user
                });
            }
        })(req, res);
    },

    /**
     * Sign up in system
     */
    signup: function (req, res) {
        // TODO: think about model duplicate

        User
            .create(req.allParams())
            .exec(function (error, user) {
                if (error) {
                    sails.log.error(error);
                    res.serverError(error);
                } else {
                    res.created({
                        token: CipherService.create('jwt', {id: user.id}).hashSync(),
                        user: user
                    });
                }
            });
    },

    /**
     * Facebook authorization\linking
     */
    facebook: function (req, res) {
        passport.authenticate('jwt', function (error, user) {
            req.user = user;

            passport.authenticate('facebook-token', function (error, user, info) {
                if (error) {
                    sails.log.error(error);
                    res.serverError(error);
                } else if (info) {
                    res.unauthorized(null, info.code, info.message);
                } else {
                    res.ok({
                        token: CipherService.create('jwt', {id: user.id}).hashSync(),
                        user: user
                    });
                }
            })(req, res);
        })(req, res);
    },

    /**
{'fraction_non_alphanumeric': 0.1279508970727101, 'fraction_numerical': 0.0, 'mean_word_length': 1.1761684643040575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '521041', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1058, 'n_words': 240}