Migrated the Session.ts model -> mongoose-field-encryption

This commit is contained in:
Mathias Wagner 2023-01-15 18:43:47 +01:00
parent bc68da563a
commit f07187691d
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,10 +1,12 @@
import {model, ObjectId, Schema, Types} from "mongoose";
import encrypt from "mongoose-encryption";
import { fieldEncryption } from "mongoose-field-encryption";
import process from "process";
interface ISession {
id: ObjectId,
userId: ObjectId,
token: string,
ip: string,
userAgent: string,
verified: boolean
}
@ -18,6 +20,7 @@ const SessionSchema = new Schema<ISession>({
type: String,
required: true
},
ip: String,
userAgent: String,
verified: {
type: Boolean,
@ -25,9 +28,10 @@ const SessionSchema = new Schema<ISession>({
}
});
SessionSchema.plugin(encrypt, {
encryptionKey: process.env.ENC_KEY,
signingKey: process.env.SIG_KEY
SessionSchema.plugin(fieldEncryption, {
fields: ["userId", "token", "ip", "userAgent"],
secret: process.env.ENC_KEY,
saltGenerator: () => process.env.SIG_KEY
});
export const Session = model<ISession>("sessions", SessionSchema);