import { AccountModel } from '../account/account'
import { ActorModel } from '../activitypub/actor'
import { clearCacheByToken } from '../../lib/oauth-model'
+import * as Bluebird from 'bluebird'
+import { MOAuthTokenUser } from '@server/typings/models/oauth/oauth-token'
export type OAuthTokenInfo = {
refreshToken: string
WITH_USER = 'WITH_USER'
}
-@Scopes({
+@Scopes(() => ({
[ScopeNames.WITH_USER]: {
include: [
{
- model: () => UserModel.unscoped(),
+ model: UserModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id' ],
- model: () => AccountModel.unscoped(),
+ model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'url' ],
- model: () => ActorModel.unscoped(),
+ model: ActorModel.unscoped(),
required: true
}
]
}
]
}
-})
+}))
@Table({
tableName: 'oAuthToken',
indexes: [
})
}
- static getByTokenAndPopulateUser (bearerToken: string) {
+ static getByTokenAndPopulateUser (bearerToken: string): Bluebird<MOAuthTokenUser> {
const query = {
where: {
accessToken: bearerToken
}
}
- return OAuthTokenModel.scope(ScopeNames.WITH_USER).findOne(query).then(token => {
- if (token) token['user'] = token.User
+ return OAuthTokenModel.scope(ScopeNames.WITH_USER)
+ .findOne(query)
+ .then(token => {
+ if (!token) return null
- return token
- })
+ return Object.assign(token, { user: token.User })
+ })
}
- static getByRefreshTokenAndPopulateUser (refreshToken: string) {
+ static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird<MOAuthTokenUser> {
const query = {
where: {
refreshToken: refreshToken
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
- if (token) {
- token['user'] = token.User
- return token
- } else {
- return new OAuthTokenModel()
- }
+ if (!token) return new OAuthTokenModel()
+
+ return Object.assign(token, { user: token.User })
})
}