aboutsummaryrefslogtreecommitdiff
path: root/src/server/authentication/DashUserModel.ts
blob: 7aa7f259895243c33692e15da4baad9b67e02c0d (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as bcrypt from 'bcrypt-nodejs';
import * as mongoose from 'mongoose';
import { Utils } from '../../Utils';

type comparePasswordFunction = (candidatePassword: string, cb: (err: Error, isMatch: boolean) => void) => void;
export type DashUserModel = mongoose.Document & {
    email?: string | null | undefined;
    password?: string | null | undefined;
    passwordResetToken?: string | null | undefined;
    passwordResetExpires?: Date | null | undefined;

    dropboxRefresh?: string | null | undefined;
    dropboxToken?: string | null | undefined;

    userDocumentId?: string | null | undefined;
    sharingDocumentId?: string | null | undefined;
    linkDatabaseId?: string | null | undefined;
    cacheDocumentIds?: string | null | undefined;
    profile?:
        | {
              name?: string | null | undefined;
              gender?: string | null | undefined;
              location?: string | null | undefined;
              website?: string | null | undefined;
              picture?: string | null | undefined;
          }
        | null
        | undefined;

    comparePassword?: comparePasswordFunction | null | undefined;
};

export type AuthToken = {
    accessToken: string;
    kind: string;
};

const userSchema = new mongoose.Schema(
    {
        email: String,
        password: String,
        passwordResetToken: String,
        passwordResetExpires: Date,

        dropboxRefresh: String,
        dropboxToken: String,
        userDocumentId: String, // id that identifies a document which hosts all of a user's account data
        sharingDocumentId: String, // id that identifies a document that stores documents shared to a user, their user color, and any additional info needed to communicate between users
        linkDatabaseId: String,
        cacheDocumentIds: String, // set of document ids to retreive on startup

        facebook: String,
        twitter: String,
        google: String,

        profile: {
            name: String,
            gender: String,
            location: String,
            website: String,
            picture: String,
        },
    },
    { timestamps: true }
);

/**
 * Password hash middleware.
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
userSchema.pre('save', function save(next: any) {
    if (!this.isModified('password')) {
        return next();
    }
    bcrypt.genSalt(
        10,
        ((err: Error, salt: string) => {
            if (err) {
                return next(err);
            }
            bcrypt.hash(
                this.password ?? '',
                salt,
                () => {},
                (cryptErr: mongoose.Error, hash: string) => {
                    if (cryptErr) {
                        return next(cryptErr);
                    }
                    this.password = hash;
                    next();
                    return undefined;
                }
            );
            return undefined;
        }).bind(this)
    );
    return undefined;
});

const comparePassword: comparePasswordFunction = function (this: DashUserModel, candidatePassword, cb) {
    // Choose one of the following bodies for authentication logic.
    // secure (expected, default)
    this.password && bcrypt.compare(candidatePassword, this.password, cb);
    // bypass password (debugging)
    // cb(undefined, true);
};

userSchema.methods.comparePassword = comparePassword;

const User = mongoose.model('User', userSchema);
export function initializeGuest() {
    new User({
        email: 'guest',
        password: 'guest',
        userDocumentId: Utils.GuestID(),
        sharingDocumentId: '2',
        linkDatabaseId: '3',
        cacheDocumentIds: '',
    }).save();
}
export default User;