diff options
Diffstat (limited to 'src/server/authentication/DashUserModel.ts')
-rw-r--r-- | src/server/authentication/DashUserModel.ts | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts index 6fd8dd593..7aa7f2598 100644 --- a/src/server/authentication/DashUserModel.ts +++ b/src/server/authentication/DashUserModel.ts @@ -3,30 +3,31 @@ import * as mongoose from 'mongoose'; import { Utils } from '../../Utils'; type comparePasswordFunction = (candidatePassword: string, cb: (err: Error, isMatch: boolean) => void) => void; -type mongooseDocument = { id: string }; // & mongoose.Document; -export type DashUserModel = mongooseDocument & { - email: string; - password: string; - passwordResetToken?: string; - passwordResetExpires?: Date; +export type DashUserModel = mongoose.Document & { + email?: string | null | undefined; + password?: string | null | undefined; + passwordResetToken?: string | null | undefined; + passwordResetExpires?: Date | null | undefined; - dropboxRefresh?: string; - dropboxToken?: string; + dropboxRefresh?: string | null | undefined; + dropboxToken?: string | null | undefined; - userDocumentId: string; - sharingDocumentId: string; - linkDatabaseId: string; - cacheDocumentIds: string; + 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; - profile: { - name: string; - gender: string; - location: string; - website: string; - picture: string; - }; - - comparePassword: comparePasswordFunction; + comparePassword?: comparePasswordFunction | null | undefined; }; export type AuthToken = { @@ -68,36 +69,38 @@ const userSchema = new mongoose.Schema( */ // eslint-disable-next-line @typescript-eslint/no-explicit-any userSchema.pre('save', function save(next: any) { - const user = this; - if (!user.isModified('password')) { + if (!this.isModified('password')) { return next(); } - bcrypt.genSalt(10, (err: Error, salt: string) => { - if (err) { - return next(err); - } - bcrypt.hash( - user.password ?? '', - salt, - () => {}, - (cryptErr: mongoose.Error, hash: string) => { - if (cryptErr) { - return next(cryptErr); - } - user.password = hash; - next(); - return undefined; + bcrypt.genSalt( + 10, + ((err: Error, salt: string) => { + if (err) { + return next(err); } - ); - return undefined; - }); + 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) - bcrypt.compare(candidatePassword, this.password, cb); + this.password && bcrypt.compare(candidatePassword, this.password, cb); // bypass password (debugging) // cb(undefined, true); }; |