<commit_before>bcrypt = require 'bcrypt'
crypto = require 'crypto'
gravatar = require 'gravatar'
mongoose = require 'mongoose'
emailModule = require '../../lib/email'

schema = new mongoose.Schema
  username: type: String, required: yes, unique: yes
  email: type: String, required: yes, unique: yes
  password: type: String, required: yes # Hash
  players:
    type: [mongoose.Schema.ObjectId]
    ref: 'Player'

schema.statics.randomPass = (cb) ->
  crypto.randomBytes 6, (ex, buf) ->
    cb buf.toString('base64').replace(/\//g, '_').replace(/\+/g, '-')

schema.statics.register = (username, email, cb) ->
  return cb 'Please enter a username.' unless username
  username = username.toLowerCase()
  return cb 'Please enter an email.' unless email
  email = email.toLowerCase()

  query =
    $or: [
      {username: username},
      {email: email}
    ]

  @findOne query, (err, user) =>
    if user
      if user.username is username
        cb 'Username is already in use.', null
      else
        cb 'Email is already in use.', null
      return
    
    @randomPass (pass) =>
      user = new this
        username: username
        email: email
<commit_msg>Add servers to user schema<commit_after>bcrypt = require 'bcrypt'
crypto = require 'crypto'
gravatar = require 'gravatar'
mongoose = require 'mongoose'
emailModule = require '../../lib/email'

schema = new mongoose.Schema
  username: type: String, required: yes, unique: yes
  email: type: String, required: yes, unique: yes
  password: type: String, required: yes # Hash
  players:
    type: [mongoose.Schema.ObjectId]
    ref: 'Player'
  servers:
    type: [mongoose.Schema.ObjectId]
    ref: 'Server'

schema.statics.randomPass = (cb) ->
  crypto.randomBytes 6, (ex, buf) ->
    cb buf.toString('base64').replace(/\//g, '_').replace(/\+/g, '-')

schema.statics.register = (username, email, cb) ->
  return cb 'Please enter a username.' unless username
  username = username.toLowerCase()
  return cb 'Please enter an email.' unless email
  email = email.toLowerCase()

  query =
    $or: [
      {username: username},
      {email: email}
    ]

  @findOne query, (err, user) =>
    if user
      if user.username is username
        cb 'Username is already in use.', null
      else
        cb 'Email is already in use.', null
      return
    
    @randomPass (pass) =>
      user = new this
        username: username
        email: email

{'fraction_non_alphanumeric': 0.12982165076731647, 'fraction_numerical': 0.002488593944421402, 'mean_word_length': 2.853035143769968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2320857', 'n_tokens_mistral': 731, 'n_tokens_neox': 742, 'n_words': 243}