<commit_before>const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const validator = require('validator');
const jwt = require('jsonwebtoken');
const _ = require('lodash');
const bcrypt = require('bcryptjs');
const config = require('../../config/config');

var UserSchema = new Schema({
    username: {
        type: String,
        lowercase: true,
        trim: true,
        minlength: 3,
        maxlength: 15,
    },
    email: {
        type: String,
        lowercase: true,
        trim: true,
        minlength: 3,
        unique: true,
        validate: {
            validator: validator.isEmail,
            message: '{VALUE} is not a valid email'
        }
    },
    password: {
        type: String,
        minlength: 6,
        select: false
    },
    picture: {
        type: String,
        default: 'https://www.hellochess.com/img/default-img.png'
    },
    name: {
        type: String,
        trim: true
    },
    social: {
        type: Boolean,
        default: false
    },
    two_elos: {
        bullet: {
            type: Number,
            default: 1200
        },
        blitz: {
            type: Number,
            default: 1200
        },
        rapid: {
            type: Number,
            default: 1200
        },
        classic: {
            type: Number,
            default: 1200
        }
    },
    four_elos: {
        bullet: {
            type: Number,
            default: 1200
        },
        blitz: {
            type: Number,
            default: 1200
        },
        rapid: {
            type: Number,
            default: 1200
        },
        classic: {
            type: Number,
            default: 1200
        }
    },
    socket_id: {
        type: String
    },
    socialProvider: {
        name: String,
        id: String
    },
    tokens: [{
        access: {
            type: String,
            required: true,
        },
        token: {
            type: String,
            required: true,
        },
        select: false
    }]
});

UserSchema.methods.toJSON = function() {
    var user = this;
    var userObject = user.toObject();

    return _.pick(userObject, [
        '_id',
        'picture',
        'email',
        'username',
        'social',
        'two_elos',
        'four_elos'
    ]);
};

UserSchema.methods.generateAuthToken = function() {
    var user = this;
    var access = 'auth';
    const timestamp = new Date().getTime();
    var token = jwt.sign({ sub: user._id.toHexString(), iat: timestamp, access}, config.secret).toString();

    user.tokens.push({access, token});

    return user.save().then(() => {<commit_msg>remove old tokens on login<commit_after>const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const validator = require('validator');
const jwt = require('jsonwebtoken');
const _ = require('lodash');
const bcrypt = require('bcryptjs');
const config = require('../../config/config');

var UserSchema = new Schema({
    username: {
        type: String,
        lowercase: true,
        trim: true,
        minlength: 3,
        maxlength: 15,
    },
    email: {
        type: String,
        lowercase: true,
        trim: true,
        minlength: 3,
        unique: true,
        validate: {
            validator: validator.isEmail,
            message: '{VALUE} is not a valid email'
        }
    },
    password: {
        type: String,
        minlength: 6,
        select: false
    },
    picture: {
        type: String,
        default: 'https://www.hellochess.com/img/default-img.png'
    },
    name: {
        type: String,
        trim: true
    },
    social: {
        type: Boolean,
        default: false
    },
    two_elos: {
        bullet: {
            type: Number,
            default: 1200
        },
        blitz: {
            type: Number,
            default: 1200
        },
        rapid: {
            type: Number,
            default: 1200
        },
        classic: {
            type: Number,
            default: 1200
        }
    },
    four_elos: {
        bullet: {
            type: Number,
            default: 1200
        },
        blitz: {
            type: Number,
            default: 1200
        },
        rapid: {
            type: Number,
            default: 1200
        },
        classic: {
            type: Number,
            default: 1200
        }
    },
    socket_id: {
        type: String
    },
    socialProvider: {
        name: String,
        id: String
    },
    tokens: [{
        access: {
            type: String,
            required: true,
        },
        token: {
            type: String,
            required: true,
        },
        select: false
    }]
});

UserSchema.methods.toJSON = function() {
    var user = this;
    var userObject = user.toObject();

    return _.pick(userObject, [
        '_id',
        'picture',
        'email',
        'username',
        'social',
        'two_elos',
        'four_elos'
    ]);
};

UserSchema.methods.generateAuthToken = function() {
    var user = this;
    var access = 'auth';
    const timestamp = new Date().getTime();
    var token = jwt.sign({ sub: user._id.toHexString(), iat: timestamp, access}, config.secret).toString();

    user.tokens = [];
    user.tokens.push({access, token});

    return user.save().then(() => {
{'fraction_non_alphanumeric': 0.12450851900393185, 'fraction_numerical': 0.013855083317730763, 'mean_word_length': 1.4182888184698959, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7352687', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1527, 'n_words': 371}