refactor(db/models): update user model to generate both access and refresh JWTs

This commit is contained in:
parent e88474d406
commit a5a3147365
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -1,4 +1,4 @@
import mongoose, { PopulatedDoc, Document, Model } from "mongoose";
import mongoose, { Document, Model, PopulatedDoc } from "mongoose";
import SequenceFactory from "mongoose-sequence";
import bcrypt from "bcryptjs";
import md5 from "blueimp-md5";
@ -7,7 +7,7 @@ import { hasMigrated } from "@dbconfig";
import { IBand } from "./band";
import { IStory } from "./stories/index";
import { QuickMenuItem, QuickMenuSchema } from "./quickMenu";
import { FilterPick, PopulatedDocKeys, PopulatedPick } from "~/utils/filter";
// import { FilterPick, PopulatedDocKeys, PopulatedPick } from "~/utils/filter";
const AutoIncrement = SequenceFactory(mongoose);
interface IIPLogEntry {
@ -70,7 +70,8 @@ export interface IUser extends Document {
quickMenuConfig: QuickMenuItem[];
notifyOnReviewReply?: boolean;
validPassword(password: string): boolean;
generateJWT(jwtSecret: string): string;
generateRefreshToken(jwtSecret: string): string;
generateAccessToken(jwtSecret: string): string;
}
// type iut = IUser;
interface UModel extends Model<IUser> {
@ -272,11 +273,16 @@ UserSchema.methods.validPassword = function (password: string): boolean {
return md5(password) === this.password || bcrypt.compareSync(password, this.password) || false;
};
UserSchema.methods.generateJWT = function (jwtKey: string): string {
let token = jwt.sign({ id: this._id, isAdmin: this.profile.isAdmin }, jwtKey, {
UserSchema.methods.generateRefreshToken = function (jwtKey: string): string {
return jwt.sign({ id: this._id, isAdmin: this.profile.isAdmin }, jwtKey, {
expiresIn: "14 days",
});
return token;
};
UserSchema.methods.generateAccessToken = function (jwtKey: string): string {
return jwt.sign({ id: this._id, isAdmin: this.profile.isAdmin }, jwtKey, {
expiresIn: "15m",
});
};
hasMigrated && !mongoose.models.User && UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" });