Server: when we remove a user, remove the oauthtokens too

pull/10/merge
Chocobozzz 2016-10-01 09:12:02 +02:00
parent 0eb78d5303
commit b81929a014
2 changed files with 14 additions and 1 deletions

View File

@ -20,7 +20,8 @@ OAuthTokenSchema.path('user').required(true)
OAuthTokenSchema.statics = { OAuthTokenSchema.statics = {
getByRefreshTokenAndPopulateClient: getByRefreshTokenAndPopulateClient, getByRefreshTokenAndPopulateClient: getByRefreshTokenAndPopulateClient,
getByTokenAndPopulateUser: getByTokenAndPopulateUser, getByTokenAndPopulateUser: getByTokenAndPopulateUser,
getByRefreshToken: getByRefreshToken getByRefreshToken: getByRefreshToken,
removeByUserId: removeByUserId
} }
mongoose.model('OAuthToken', OAuthTokenSchema) mongoose.model('OAuthToken', OAuthTokenSchema)
@ -53,3 +54,7 @@ function getByTokenAndPopulateUser (bearerToken) {
function getByRefreshToken (refreshToken) { function getByRefreshToken (refreshToken) {
return this.findOne({ refreshToken: refreshToken }).exec() return this.findOne({ refreshToken: refreshToken }).exec()
} }
function removeByUserId (userId, callback) {
return this.remove({ user: userId }, callback)
}

View File

@ -4,6 +4,8 @@ const customUsersValidators = require('../helpers/custom-validators').users
const modelUtils = require('./utils') const modelUtils = require('./utils')
const peertubeCrypto = require('../helpers/peertube-crypto') const peertubeCrypto = require('../helpers/peertube-crypto')
const OAuthToken = mongoose.model('OAuthToken')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const UserSchema = mongoose.Schema({ const UserSchema = mongoose.Schema({
@ -46,6 +48,12 @@ UserSchema.pre('save', function (next) {
}) })
}) })
UserSchema.pre('remove', function (next) {
const user = this
OAuthToken.removeByUserId(user._id, next)
})
mongoose.model('User', UserSchema) mongoose.model('User', UserSchema)
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------