diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/passport.js | 8 | ||||
| -rw-r--r-- | lib/sockets.js | 26 | ||||
| -rw-r--r-- | lib/utils.js | 29 |
3 files changed, 33 insertions, 30 deletions
diff --git a/lib/passport.js b/lib/passport.js index 1e4fd8b8..81f4e948 100644 --- a/lib/passport.js +++ b/lib/passport.js @@ -1,5 +1,6 @@ const Passport = require("passport"); const jwt = require("passport-jwt"); +const { getUser } = require("./utils"); function cookieExtractor(req) { if (req && req.cookies) return req.cookies["token"]; @@ -17,12 +18,7 @@ passport = Passport.use( }, async (payload, next) => { try { - let user = await db.User.findOne({ - where: { - username: payload.username - } - }); - next(null, user); + next(null, await getUser(payload.username)); } catch (e) { l.error("error finding user for session", e); next(null, { username: "guest" }); diff --git a/lib/sockets.js b/lib/sockets.js index f35ca5f5..ad7295b0 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -1,6 +1,6 @@ const jwt = require("jsonwebtoken"); const io = require("socket.io"); -const { Op } = require("sequelize"); +const { getUser } = require("./utils"); /* eslint-disable-next-line */ const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); @@ -38,29 +38,7 @@ socket.sockets.on("connect", async socket => { if (app.get("rates")) socket.emit("rate", app.get("rates").ask); socket.on("getuser", async (data, callback) => { l.info("socket open", socket.request.user); - const user = await db.User.findOne({ - include: [ - { - model: db.Payment, - as: "payments", - order: [["id", "DESC"]], - where: { - [Op.or]: { - received: true, - amount: { - [Op.lt]: 0, - }, - }, - }, - limit: 12 - } - ], - where: { - username: socket.request.user - } - }); - - callback(user); + callback(await getUser(socket.request.user)); }); socket.on("disconnect", s => { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..c6e02a1a --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,29 @@ +const { Op } = require("sequelize"); + +module.exports = { + async getUser(username) { + return db.User.findOne({ + include: [ + { + model: db.Payment, + as: "payments", + order: [["id", "DESC"]], + where: { + [Op.or]: { + received: true, + amount: { + [Op.lt]: 0 + } + } + }, + limit: 12 + }, + { + model: db.Account, + as: "accounts" + } + ], + where: { username } + }); + } +}; |
