api: fix more mongoose stuff
This commit is contained in:
parent
50d6697b5b
commit
0784f916b6
|
@ -38,6 +38,7 @@ if (!isProduction) {
|
|||
}
|
||||
|
||||
require('./db');
|
||||
require('./models');
|
||||
|
||||
app.use(require('./routes'));
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ const schema = new mongoose.Schema(
|
|||
|
||||
schema.plugin(uniqueValidator, { message: 'reused token' });
|
||||
|
||||
class AccessToken extends mongoose.Model {
|
||||
class AccessTokenClass extends mongoose.Model {
|
||||
toJSON() {
|
||||
return {
|
||||
token: this.token,
|
||||
|
@ -24,7 +24,7 @@ class AccessToken extends mongoose.Model {
|
|||
}
|
||||
|
||||
isValid() {
|
||||
return this.expiresAt < new Date()
|
||||
return this.expiresAt < new Date();
|
||||
}
|
||||
|
||||
toHeaderString() {
|
||||
|
@ -42,6 +42,8 @@ class AccessToken extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(AccessToken, schema);
|
||||
schema.loadClass(AccessTokenClass);
|
||||
|
||||
const AccessToken = mongoose.model('AccessToken', schema);
|
||||
|
||||
module.exports = AccessToken;
|
||||
|
|
|
@ -14,7 +14,7 @@ const schema = new mongoose.Schema(
|
|||
{ timestamps: true },
|
||||
);
|
||||
|
||||
class AuthorizationCode extends mongoose.Model {
|
||||
class AuthorizationCodeClass extends mongoose.Model {
|
||||
static generate(options, expiresInSeconds = 60) {
|
||||
const code = crypto.randomBytes(8).toString('hex');
|
||||
|
||||
|
@ -26,6 +26,7 @@ class AuthorizationCode extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(AuthorizationCode, schema);
|
||||
schema.loadClass(AuthorizationCodeClass)
|
||||
|
||||
module.exports = AuthorizationCode;
|
||||
const AuthorizationCode = mongoose.model('AuthorizationCode', schema);
|
||||
module.exports = AuthorizationCode
|
||||
|
|
|
@ -9,7 +9,7 @@ const schema = new mongoose.Schema(
|
|||
{ timestamps: true },
|
||||
);
|
||||
|
||||
class Comment extends mongoose.Model {
|
||||
class CommentClass extends mongoose.Model {
|
||||
toJSONFor(user) {
|
||||
return {
|
||||
id: this._id,
|
||||
|
@ -20,6 +20,5 @@ class Comment extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(Comment, schema);
|
||||
|
||||
module.exports = Comment;
|
||||
schema.loadClass(CommentClass)
|
||||
module.exports = mongoose.model('Comment', schema);
|
||||
|
|
|
@ -17,7 +17,7 @@ const schema = new mongoose.Schema(
|
|||
|
||||
schema.plugin(uniqueValidator, { message: 'reused token' });
|
||||
|
||||
class RefreshToken extends mongoose.Model {
|
||||
class RefreshTokenClass extends mongoose.Model {
|
||||
toJSON() {
|
||||
return {
|
||||
token: this.token,
|
||||
|
@ -40,6 +40,7 @@ class RefreshToken extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(RefreshToken, schema);
|
||||
schema.loadClass(RefreshTokenClass);
|
||||
|
||||
module.exports = RefreshToken;
|
||||
const RefreshToken = mongoose.model('RefreshToken', schema);
|
||||
module.exports = RefreshToken
|
||||
|
|
|
@ -142,7 +142,7 @@ function getDaytime(dateTime) {
|
|||
return DAYTIMES[Math.floor((dateTime.hour % 24) / 2)];
|
||||
}
|
||||
|
||||
class Track extends mongoose.Model {
|
||||
class TrackClass extends mongoose.Model {
|
||||
slugify() {
|
||||
this.slug = slug(this.title || 'track') + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36);
|
||||
}
|
||||
|
@ -301,6 +301,6 @@ class Track extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(Track, schema);
|
||||
|
||||
schema.loadClass(TrackClass);
|
||||
const Track = mongoose.model('Track', schema);
|
||||
module.exports = Track;
|
||||
|
|
|
@ -39,7 +39,7 @@ const schema = new mongoose.Schema(
|
|||
|
||||
schema.plugin(uniqueValidator, { message: 'ist bereits vergeben. Sorry!' });
|
||||
|
||||
class User extends mongoose.Model {
|
||||
class UserClass extends mongoose.Model {
|
||||
validPassword(password) {
|
||||
const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
return this.hash === hash;
|
||||
|
@ -86,6 +86,6 @@ class User extends mongoose.Model {
|
|||
}
|
||||
}
|
||||
|
||||
mongoose.model(User, schema);
|
||||
|
||||
module.exports = User;
|
||||
schema.loadClass(UserClass);
|
||||
const User = mongoose.model('User', schema);
|
||||
module.exports = User
|
||||
|
|
Loading…
Reference in a new issue