mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-03-01 05:29:41 +00:00
Removed index definition from Session model since the migration already creates it. This prevents .sync() from trying to create the index before migrations run. Also bumped version to 2.34.0 to match migration files. Fixes: SQLITE_ERROR: no such column: oidcSessionId
94 lines
2.1 KiB
JavaScript
94 lines
2.1 KiB
JavaScript
const { DataTypes, Model, Op } = require('sequelize')
|
|
|
|
class Session extends Model {
|
|
constructor(values, options) {
|
|
super(values, options)
|
|
|
|
/** @type {UUIDV4} */
|
|
this.id
|
|
/** @type {string} */
|
|
this.ipAddress
|
|
/** @type {string} */
|
|
this.userAgent
|
|
/** @type {Date} */
|
|
this.createdAt
|
|
/** @type {Date} */
|
|
this.updatedAt
|
|
/** @type {UUIDV4} */
|
|
this.userId
|
|
/** @type {Date} */
|
|
this.expiresAt
|
|
/** @type {string} */
|
|
this.oidcIdToken
|
|
/** @type {string} */
|
|
this.oidcSessionId
|
|
|
|
// Expanded properties
|
|
|
|
/** @type {import('./User').User} */
|
|
this.user
|
|
}
|
|
|
|
static async createSession(userId, ipAddress, userAgent, refreshToken, expiresAt, oidcIdToken = null, oidcSessionId = null) {
|
|
const session = await Session.create({ userId, ipAddress, userAgent, refreshToken, expiresAt, oidcIdToken, oidcSessionId })
|
|
return session
|
|
}
|
|
|
|
/**
|
|
* Clean up expired sessions from the database
|
|
* @returns {Promise<number>} Number of sessions deleted
|
|
*/
|
|
static async cleanupExpiredSessions() {
|
|
const deletedCount = await Session.destroy({
|
|
where: {
|
|
expiresAt: {
|
|
[Op.lt]: new Date()
|
|
}
|
|
}
|
|
})
|
|
return deletedCount
|
|
}
|
|
|
|
/**
|
|
* Initialize model
|
|
* @param {import('../Database').sequelize} sequelize
|
|
*/
|
|
static init(sequelize) {
|
|
super.init(
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true
|
|
},
|
|
ipAddress: DataTypes.STRING,
|
|
userAgent: DataTypes.STRING,
|
|
refreshToken: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
expiresAt: {
|
|
type: DataTypes.DATE,
|
|
allowNull: false
|
|
},
|
|
oidcIdToken: DataTypes.TEXT,
|
|
oidcSessionId: DataTypes.STRING
|
|
},
|
|
{
|
|
sequelize,
|
|
modelName: 'session'
|
|
}
|
|
)
|
|
|
|
const { user } = sequelize.models
|
|
user.hasMany(Session, {
|
|
onDelete: 'CASCADE',
|
|
foreignKey: {
|
|
allowNull: false
|
|
}
|
|
})
|
|
Session.belongsTo(user)
|
|
}
|
|
}
|
|
|
|
module.exports = Session
|