aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.js
blob: 5da5da41703a0656929a2b2ba88e2fff373f4722 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const mongoose = require('mongoose')

// ---------------------------------------------------------------------------

const OAuthTokenSchema = mongoose.Schema({
  accessToken: String,
  accessTokenExpiresOn: Date,
  client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' },
  refreshToken: String,
  refreshTokenExpiresOn: Date,
  user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
})

OAuthTokenSchema.path('accessToken').required(true)
OAuthTokenSchema.path('client').required(true)
OAuthTokenSchema.path('user').required(true)

OAuthTokenSchema.statics = {
  loadByRefreshToken: loadByRefreshToken,
  loadByTokenAndPopulateUser: loadByTokenAndPopulateUser
}

mongoose.model('OAuthToken', OAuthTokenSchema)

// ---------------------------------------------------------------------------

function loadByRefreshToken (refreshToken, callback) {
  return this.findOne({ refreshToken: refreshToken }, callback)
}

function loadByTokenAndPopulateUser (bearerToken, callback) {
  // FIXME: allow to use callback
  return this.findOne({ accessToken: bearerToken }).populate('user')
}