refactor(db/models): update user model to generate both access and refresh JWTs
This commit is contained in:
parent
e88474d406
commit
a5a3147365
@ -1,4 +1,4 @@
|
|||||||
import mongoose, { PopulatedDoc, Document, Model } from "mongoose";
|
import mongoose, { Document, Model, PopulatedDoc } from "mongoose";
|
||||||
import SequenceFactory from "mongoose-sequence";
|
import SequenceFactory from "mongoose-sequence";
|
||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import md5 from "blueimp-md5";
|
import md5 from "blueimp-md5";
|
||||||
@ -7,7 +7,7 @@ import { hasMigrated } from "@dbconfig";
|
|||||||
import { IBand } from "./band";
|
import { IBand } from "./band";
|
||||||
import { IStory } from "./stories/index";
|
import { IStory } from "./stories/index";
|
||||||
import { QuickMenuItem, QuickMenuSchema } from "./quickMenu";
|
import { QuickMenuItem, QuickMenuSchema } from "./quickMenu";
|
||||||
import { FilterPick, PopulatedDocKeys, PopulatedPick } from "~/utils/filter";
|
// import { FilterPick, PopulatedDocKeys, PopulatedPick } from "~/utils/filter";
|
||||||
|
|
||||||
const AutoIncrement = SequenceFactory(mongoose);
|
const AutoIncrement = SequenceFactory(mongoose);
|
||||||
interface IIPLogEntry {
|
interface IIPLogEntry {
|
||||||
@ -70,7 +70,8 @@ export interface IUser extends Document {
|
|||||||
quickMenuConfig: QuickMenuItem[];
|
quickMenuConfig: QuickMenuItem[];
|
||||||
notifyOnReviewReply?: boolean;
|
notifyOnReviewReply?: boolean;
|
||||||
validPassword(password: string): boolean;
|
validPassword(password: string): boolean;
|
||||||
generateJWT(jwtSecret: string): string;
|
generateRefreshToken(jwtSecret: string): string;
|
||||||
|
generateAccessToken(jwtSecret: string): string;
|
||||||
}
|
}
|
||||||
// type iut = IUser;
|
// type iut = IUser;
|
||||||
interface UModel extends Model<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;
|
return md5(password) === this.password || bcrypt.compareSync(password, this.password) || false;
|
||||||
};
|
};
|
||||||
|
|
||||||
UserSchema.methods.generateJWT = function (jwtKey: string): string {
|
UserSchema.methods.generateRefreshToken = function (jwtKey: string): string {
|
||||||
let token = jwt.sign({ id: this._id, isAdmin: this.profile.isAdmin }, jwtKey, {
|
return jwt.sign({ id: this._id, isAdmin: this.profile.isAdmin }, jwtKey, {
|
||||||
expiresIn: "14 days",
|
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" });
|
hasMigrated && !mongoose.models.User && UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" });
|
||||||
|
Loading…
Reference in New Issue
Block a user