<commit_before>/**
 * Passport configuration file where you should configure all your strategies
 * @description :: Configuration file where you configure your passport authentication
 */

// TODO: add more strategies
var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    JwtStrategy = require('passport-jwt').Strategy,
    FacebookTokenStrategy = require('passport-facebook-token').Strategy,
    TwitterTokenStrategy = require('passport-twitter-token').Strategy;

passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password'
}, function (username, password, next) {
    User
        .findOne({
            or: [{
                username: username
            }, {
                email: username
            }]
        })
        .exec(function (error, user) {
            if (error) {
                return next(error);
            }

            if (!user) {
                return next(null, false, {
                    code: "E_USER_NOT_FOUND",
                    message: username + " is not found"
                });
            }

            if (!CipherService.create('bcrypt', user.password).compareSync(password)) {
                return next(null, false, {
                    code: "E_WRONG_PASSWORD",
                    message: "Password is wrong"
                });
            }

            return next(null, user);
        });
}));

passport.use(new JwtStrategy("<%= answers['application:jwt-secret'] %>", {
    tokenBodyField: 'jwt-token',
    tokenHeader: 'JWT'
}, function (payload, next) {
    User
        .findOne({
            id: payload.id
        })
        .exec(function (error, user) {
            if (error) {
                return next(error);
            }

            if (!user) {
                return next(null, false, {
                    code: "E_USER_NOT_FOUND",
                    message: "User with that JWT not found"
                });
            }

            return next(null, user);
        });
}));

passport.use(new FacebookTokenStrategy({
    clientID: "<%= answers['application:facebook-client-id'] %>",
    clientSecret: "<%= answers['application:facebook-client-secret'] %>",
    passReqToCallback: true
}, function (req, accessToken, refreshToken, profile, next) {
    if (!req.user) {
        User
            .findOrCreate({
                'facebook.id': profile.id
            }, {
                username: req.param('username') || profile.username || profile.displayName,
                email: req.param('email') || (profile.emails && profile.emails[0].value),
                firstName: req.param('firstName') || (profile.displayName && profile.displayName.split(' ')[0]),
                lastName: req.param('lastName') || (profile.displayName && profile.displayName.split(' ')[1]),
                photo: req.param('photo') || ['https://graph.facebook.com/', profile.id, '/picture?type=large'].join(''),
                facebook: profile._json
            })
            .exec(function (error, user) {
                if (error) {
                    return next(error);
                }

                if (!user) {
                    return next(null, false, {
                        code: "E_AUTH_FAILED",
                        message: "Facebook auth failed"
                    });
                }
<commit_msg>Fix rendering passport'<commit_after>/**
 * Passport configuration file where you should configure all your strategies
 * @description :: Configuration file where you configure your passport authentication
 */

// TODO: add more strategies
var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    JwtStrategy = require('passport-jwt').Strategy,
    FacebookTokenStrategy = require('passport-facebook-token').Strategy,
    TwitterTokenStrategy = require('passport-twitter-token').Strategy;

passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password'
}, function (username, password, next) {
    User
        .findOne({
            or: [{
                username: username
            }, {
                email: username
            }]
        })
        .exec(function (error, user) {
            if (error) {
                return next(error);
            }

            if (!user) {
                return next(null, false, {
                    code: "E_USER_NOT_FOUND",
                    message: username + " is not found"
                });
            }

            if (!CipherService.create('bcrypt', user.password).compareSync(password)) {
                return next(null, false, {
                    code: "E_WRONG_PASSWORD",
                    message: "Password is wrong"
                });
            }

            return next(null, user);
        });
}));

passport.use(new JwtStrategy("<%= answers['application:jwt-secret'] %>", {
    tokenBodyField: 'jwt-token',
    tokenHeader: 'JWT'
}, function (payload, next) {
    User
        .findOne({
            id: payload.id
        })
        .exec(function (error, user) {
            if (error) {
                return next(error);
            }

            if (!user) {
                return next(null, false, {
                    code: "E_USER_NOT_FOUND",
                    message: "User with that JWT not found"
                });
            }

            return next(null, user);
        });
}));

passport.use(new FacebookTokenStrategy({
    clientID: "<%= answers['application:facebook-app-id'] %>",
    clientSecret: "<%= answers['application:facebook-app-secret'] %>",
    passReqToCallback: true
}, function (req, accessToken, refreshToken, profile, next) {
    if (!req.user) {
        User
            .findOrCreate({
                'facebook.id': profile.id
            }, {
                username: req.param('username') || profile.username || profile.displayName,
                email: req.param('email') || (profile.emails && profile.emails[0].value),
                firstName: req.param('firstName') || (profile.displayName && profile.displayName.split(' ')[0]),
                lastName: req.param('lastName') || (profile.displayName && profile.displayName.split(' ')[1]),
                photo: req.param('photo') || ['https://graph.facebook.com/', profile.id, '/picture?type=large'].join(''),
                facebook: profile._json
            })
            .exec(function (error, user) {
                if (error) {
                    return next(error);
                }

                if (!user) {
                    return next(null, false, {
                        code: "E_AUTH_FAILED",
                        message: "Facebook auth failed"
                    });
                }

{'fraction_non_alphanumeric': 0.13290952099349498, 'fraction_numerical': 0.0008870490833826139, 'mean_word_length': 1.670746150809317, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '7297762', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1691, 'n_words': 376}