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