server: fix sessions store not being in koa-session middleware

This commit is contained in:
Katalina / stardust 2017-12-27 00:51:14 -06:00
parent 6ec6123fa9
commit e36be9e381
3 changed files with 14 additions and 15 deletions

View file

@ -11,13 +11,10 @@ module.exports = (R, $) => {
console.log(ctx.session.expiresAt >= new Date(), ctx.session.expiresAt, new Date()) console.log(ctx.session.expiresAt >= new Date(), ctx.session.expiresAt, new Date())
if (ctx.session.accessToken === undefined || ctx.session.expiresAt >= new Date()) { if (ctx.session.accessToken === undefined || ctx.session.expiresAt >= new Date()) {
console.log('getting auth token')
const data = await $.discord.getAuthToken(token) const data = await $.discord.getAuthToken(token)
console.log(data)
ctx.session.accessToken = data.access_token ctx.session.accessToken = data.access_token
ctx.session.refreshToken = data.refresh_token ctx.session.refreshToken = data.refresh_token
ctx.session.expiresAt = new Date() + ctx.expires_in ctx.session.expiresAt = new Date() + ctx.expires_in
console.log(ctx.session)
} }
const user = await $.discord.getUser(ctx.session.accessToken) const user = await $.discord.getUser(ctx.session.accessToken)

View file

@ -19,17 +19,12 @@ app.keys = [ process.env.APP_KEY ]
const DEVEL = process.env.NODE_ENV === 'development' const DEVEL = process.env.NODE_ENV === 'development'
async function start () { async function start () {
await M.awaitServices()
// body parser // body parser
const bodyParser = require('koa-bodyparser') const bodyParser = require('koa-bodyparser')
app.use(bodyParser({ types: ['json'] })) app.use(bodyParser({ types: ['json'] }))
const session = require('koa-session')
app.use(session({
key: 'roleypoly:sess',
maxAge: 'session',
store: M.sessions
}, app))
// Request logger // Request logger
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
let timeStart = new Date() let timeStart = new Date()
@ -52,11 +47,15 @@ async function start () {
return null return null
}) })
// Construct the Roleypoly! const session = require('koa-session')
app.use(session({
key: 'roleypoly:sess',
maxAge: 'session',
store: M.ctx.sessions
}, app))
await M.mountRoutes() await M.mountRoutes()
// Start it!
await M.awaitServices()
log.info(`starting HTTP server on ${process.env.APP_PORT || 6769}`) log.info(`starting HTTP server on ${process.env.APP_PORT || 6769}`)
server.listen(process.env.APP_PORT || 6769) server.listen(process.env.APP_PORT || 6769)
} }

View file

@ -29,9 +29,12 @@ class SessionsService extends Service {
} }
async destroy (id) { async destroy (id) {
return (await this.Session.findOne({ where: { id } })).destroy() const sess = await this.Session.findOne({ where: { id } })
}
if (sess != null) {
return sess.destroy()
}
}
} }
module.exports = SessionsService module.exports = SessionsService