<commit_before>var config = require(__dirname + '/../config/config'),
    mysql = require(__dirname + '/../lib/mysql'),
    logger = require(__dirname + '/../lib/logger'),
    curl = require(__dirname + '/../lib/curl'),
    as_helper = require(__dirname + '/../helpers/auth_server'),
    util = require(__dirname + '/../helpers/util'),
    googleapis = require('googleapis'),
    OAuth2 = googleapis.auth.OAuth2,
	oauth2_client = new OAuth2(config.google_auth.client_id, config.google_auth.client_secret, config.google_auth.callback_URL);

exports.register = function (req, res, next) {
    var data = req.body;
    data.app_id = config.app_id;
    data.roles = 'all,staff';

    logger.log('info', 'Someone is trying to register');
    res.clearCookie('data');

    curl.post
        .to(config.auth_server.host, config.auth_server.port, '/user/register')
        .send(data)
        .then(res.send.bind(res))
        .onerror(next);
};

exports.google_auth = function (req, res, next) {
    res.redirect(oauth2_client.generateAuthUrl({
        state : 'google',
        access_type: 'offline',
        approval_prompt : 'force',
        scope : [
            'profile',
            'email'
        ].join(' ')
    }));
};

exports.auth_google_callback = function (req, res, next) {
    var tokens,
        send_response = function (err, data) {
            if (err) return next(err);
            res.cookie('access_token', data, { signed : true });
            res.redirect(config.frontend_server_url + '/overview');
        },
        done = function (err, user, info) {
            if (err) return next(err);

            switch (info) {
                case 0 :
                    as_helper.get_access_token({
                        user_id : user.user_data._id,
                        scope_token : user.scope_token,
                        scopes : user.user_data['data_' + config.app_id].roles.map(function (a) {
                                    return config.scopes[a];
                                }).join(',')
                    }, send_response);
                    break;
                case 1 :
                    if (~user.email.indexOf('@pages.plusgoogle.com'))
                        user.email = '';
                    res.setHeader('X-Google-Data', JSON.stringify(user));
                    res.redirect(config.frontend_server_url + '/auth/register');
            }
        },
        login_to_AS = function (err, response) {
            if (err) return next(err);
            as_helper.login(response, tokens.access_token, tokens.refresh_token, done)
        },
        get_client = function (err, client) {
            if (err) return next(err);
            client
                .oauth2.userinfo.get()
                .withAuthClient(oauth2_client)
                .execute(login_to_AS);
        },
        get_tokens = function(err, _tokens) {
            if (err) return next(err);
            tokens = _tokens;
            oauth2_client.setCredentials(_tokens);
            googleapis
                .discover('oauth2', 'v2')
                .execute(get_client);
        };

    // @override<commit_msg>Put google data back on cookies<commit_after>var config = require(__dirname + '/../config/config'),
    mysql = require(__dirname + '/../lib/mysql'),
    logger = require(__dirname + '/../lib/logger'),
    curl = require(__dirname + '/../lib/curl'),
    as_helper = require(__dirname + '/../helpers/auth_server'),
    util = require(__dirname + '/../helpers/util'),
    googleapis = require('googleapis'),
    OAuth2 = googleapis.auth.OAuth2,
	oauth2_client = new OAuth2(config.google_auth.client_id, config.google_auth.client_secret, config.google_auth.callback_URL);

exports.register = function (req, res, next) {
    var data = req.body;
    data.app_id = config.app_id;
    data.roles = 'all,staff';

    logger.log('info', 'Someone is trying to register');
    res.clearCookie('data');

    curl.post
        .to(config.auth_server.host, config.auth_server.port, '/user/register')
        .send(data)
        .then(res.send.bind(res))
        .onerror(next);
};

exports.google_auth = function (req, res, next) {
    res.redirect(oauth2_client.generateAuthUrl({
        state : 'google',
        access_type: 'offline',
        approval_prompt : 'force',
        scope : [
            'profile',
            'email'
        ].join(' ')
    }));
};

exports.auth_google_callback = function (req, res, next) {
    var tokens,
        send_response = function (err, data) {
            if (err) return next(err);
            res.cookie('access_token', data, { signed : true });
            res.redirect(config.frontend_server_url + '/overview');
        },
        done = function (err, user, info) {
            if (err) return next(err);

            switch (info) {
                case 0 :
                    as_helper.get_access_token({
                        user_id : user.user_data._id,
                        scope_token : user.scope_token,
                        scopes : user.user_data['data_' + config.app_id].roles.map(function (a) {
                                    return config.scopes[a];
                                }).join(',')
                    }, send_response);
                    break;
                case 1 :
                    if (~user.email.indexOf('@pages.plusgoogle.com'))
                        user.email = '';
                    res.cookie('data', JSON.stringify(user));
                    res.redirect(config.frontend_server_url + '/register');
            }
        },
        login_to_AS = function (err, response) {
            if (err) return next(err);
            as_helper.login(response, tokens.access_token, tokens.refresh_token, done)
        },
        get_client = function (err, client) {
            if (err) return next(err);
            client
                .oauth2.userinfo.get()
                .withAuthClient(oauth2_client)
                .execute(login_to_AS);
        },
        get_tokens = function(err, _tokens) {
            if (err) return next(err);
            tokens = _tokens;
            oauth2_client.setCredentials(_tokens);
            googleapis
                .discover('oauth2', 'v2')
                .execute(get_client);
        };

    // @override
{'fraction_non_alphanumeric': 0.13328038125496425, 'fraction_numerical': 0.003812549642573471, 'mean_word_length': 1.7338254450716457, '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': '614214', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1760, 'n_words': 336}