• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dreamsparkx / node-ts-starter / 19900740412

03 Dec 2025 04:15PM UTC coverage: 80.539% (+0.08%) from 80.458%
19900740412

Pull #1349

github

Gaurav Bharti
fix(mongoose): sadqwe
Pull Request #1349: fix(mongoose): sadqwe

65 of 90 branches covered (72.22%)

Branch coverage included in aggregate %.

4 of 5 new or added lines in 1 file covered. (80.0%)

593 of 727 relevant lines covered (81.57%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/models/User.ts
1
import bcrypt from "bcrypt";
1✔
2
import mongoose from "mongoose";
1✔
3
import { generateJWT } from "../util/jwt";
1✔
4

5
export type UserDocument = mongoose.Document & {
6
  email: string;
7
  password: string;
8
  comparePassword: comparePasswordFunction;
9
  generateJWTToken: generateJWTTokenFunction;
10
  createdAt: Date;
11
  updatedAt: Date;
12
};
13
/**
14
 *
15
 * @param userPassword - the password that user inputs
16
 * @param cb - callback function that results error if there is one or returns isMatch
17
 */
18
export type comparePasswordFunction = (
19
  userPassword: string,
20
  cb: (err: Error, isMatch: boolean) => void,
21
) => void;
22

23
export type generateJWTTokenFunction = () => string;
24

25
const userSchema = new mongoose.Schema<UserDocument>(
1✔
26
  {
1✔
27
    email: { type: String, unique: true, required: true },
1✔
28
    password: { type: String, required: true },
1✔
29
  },
1✔
30
  {
1✔
31
    timestamps: true,
1✔
32
  },
1✔
33
);
1✔
34

35
userSchema.pre("save", async function () {
1✔
36
  const user = this as UserDocument;
2✔
37

38
  if (!user.isModified("password")) {
2!
NEW
39
    return;
×
40
  }
×
41

42
  const salt = await bcrypt.genSalt(10);
2✔
43
  const hash = await bcrypt.hash(user.password, salt);
2✔
44

45
  user.password = hash;
2✔
46
});
2✔
47
/**
48
 * @param userPassword text
49
 * @param cb function
50
 */
51
const comparePassword: comparePasswordFunction = function (
1✔
52
  userPassword,
3✔
53
  cb,
3✔
54
) {
3✔
55
  bcrypt.compare(userPassword, this.password, (err, isMatch) => {
3✔
56
    cb(err, isMatch);
3✔
57
  });
3✔
58
};
3✔
59

60
const generateJWTToken: generateJWTTokenFunction = function () {
1✔
61
  return generateJWT({
2✔
62
    _id: this._id,
2✔
63
    email: this.email,
2✔
64
  });
2✔
65
};
2✔
66

67
userSchema.methods.comparePassword = comparePassword;
1✔
68
userSchema.methods.generateJWTToken = generateJWTToken;
1✔
69

70
export const User = mongoose.model<UserDocument>("User", userSchema);
1✔
71
export type JWTUser = {
72
  _id: string;
73
  email: string;
74
  iat: number;
75
  exp: number;
76
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc