<commit_before>'use strict';

const express = require('express'),
    router = express.Router(),
    passport = require('passport'),
    FacebookStrategy = require('passport-facebook').Strategy,
    config = require('../config'),
    querystring = require('querystring'),
    User = require('../model/user'),
    generator = require('../strategy/token-generator');

let protocol = config.https ? 'https' : 'http',
    baseUrl = `${protocol}://${config.hostname}`;

if (process.env.NODE_ENV !== 'production') {
    baseUrl = baseUrl.concat(`:${config.port}`);
}

const facebookOptions = {
    clientID: config.facebook.appId,
    clientSecret: config.facebook.appSecret,
    callbackURL: `${baseUrl}/authenticate/facebook/callback`,
    profileFields: ['id', 'displayName', 'photos', 'email']
};

passport.use(new FacebookStrategy(facebookOptions, facebookStrategyCallback));

router.get('/', getAuthorize);
router.get('/callback', passport.authenticate('facebook'), getCallback);

module.exports = router;

function facebookStrategyCallback (accessToken, refreshToken, profile, cb) {
    let picture, email;
    if (profile._json.picture) {
        picture = profile._json.picture.data.url;
    }
    if (profile._json.email) {
        email = profile._json.email;
    }
    delete profile._json;
    delete profile._raw;
    profile.accessToken = accessToken;
    profile.refreshToken = refreshToken;

    // TODO: Use the `User.findOneAndUpdate` with upsert:true
    User.findOrCreate(<commit_msg>Allow to update facebook redirect URI<commit_after>'use strict';

const express = require('express'),
    router = express.Router(),
    passport = require('passport'),
    FacebookStrategy = require('passport-facebook').Strategy,
    config = require('../config'),
    querystring = require('querystring'),
    User = require('../model/user'),
    generator = require('../strategy/token-generator');

let protocol = config.https ? 'https' : 'http',
    baseUrl = `${protocol}://${config.hostname}`;

if (process.env.NODE_ENV !== 'production') {
    baseUrl = baseUrl.concat(`:${config.port}`);
}

const facebookOptions = {
    clientID: config.facebook.appId,
    clientSecret: config.facebook.appSecret,
    callbackURL: `${baseUrl}/authenticate/facebook/callback`,
    profileFields: ['id', 'displayName', 'photos', 'email']
};

let facebookStrategy = new FacebookStrategy(facebookOptions, facebookStrategyCallback);
passport.use(facebookStrategy);

router.get('/', getAuthorize);
router.get('/callback', updateCallbackUrl, passport.authenticate('facebook'), getCallback);

module.exports = router;

function updateCallbackUrl(req, res, next) {
    if (req.query.redirect_uri) {
        facebookStrategy._callbackURL = req.query.redirect_uri;
    }
    next();
}

function facebookStrategyCallback (accessToken, refreshToken, profile, cb) {
    let picture, email;
    if (profile._json.picture) {
        picture = profile._json.picture.data.url;
    }
    if (profile._json.email) {
        email = profile._json.email;
    }
    delete profile._json;
    delete profile._raw;
    profile.accessToken = accessToken;
    profile.refreshToken = refreshToken;

    // TODO: Use the `User.findOneAndUpdate` with upsert:true
    User.findOrCreate(
{'fraction_non_alphanumeric': 0.1487985212569316, 'fraction_numerical': 0.0, 'mean_word_length': 4.1295418641390205, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652831', 'n_tokens_mistral': 937, 'n_tokens_neox': 922, 'n_words': 189}