aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/User.js
blob: 139460ccf2a948e7a139646738168ed44b77fa6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import mongoose from 'mongoose';
import crypto from 'crypto';

function generateToken() {
    return crypto.randomBytes(32).toString('hex');
}

const userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        unique: true,
        minlength: 3,
        maxlength: 30
    },
    displayName: {
        type: String,
        maxlength: 100
    },
    token: {
        type: String,
        unique: true,
        default: generateToken
    }
}, {
    timestamps: true
});

const User = mongoose.model('User', userSchema);

export default User;
export { generateToken };