import { Schema, model, Document, Types } from 'mongoose'; export interface IRefreshToken extends Document { userId: Types.ObjectId; tokenHash: string; familyId: string; isRevoked: boolean; replacedByTokenHash?: string; userAgent?: string; ipAddress?: string; expiresAt: Date; createdAt: Date; } const refreshTokenSchema = new Schema( { userId: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true }, tokenHash: { type: String, required: true, unique: true, index: true }, familyId: { type: String, required: true, index: true }, isRevoked: { type: Boolean, default: false }, replacedByTokenHash: { type: String, default: null }, userAgent: { type: String, default: null }, ipAddress: { type: String, default: null }, expiresAt: { type: Date, required: true }, createdAt: { type: Date, default: Date.now } }, { timestamps: false } ); // TTL index to automatically delete expired refresh tokens refreshTokenSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 }); export const RefreshToken = model('RefreshToken', refreshTokenSchema);