2021-04-07 12:33:16 +02:00
|
|
|
const passport = require("koa-passport")
|
|
|
|
const LocalStrategy = require("passport-local").Strategy
|
|
|
|
const JwtStrategy = require("passport-jwt").Strategy
|
|
|
|
// const GoogleStrategy = require("passport-google-oauth").Strategy
|
2021-04-20 18:17:44 +02:00
|
|
|
const { setDB, getDB } = require("./db")
|
2021-04-07 12:33:16 +02:00
|
|
|
const { StaticDatabases } = require("./db/utils")
|
2021-04-14 17:44:10 +02:00
|
|
|
const { jwt, local, authenticated } = require("./middleware")
|
2021-04-07 12:33:16 +02:00
|
|
|
// Strategies
|
|
|
|
passport.use(new LocalStrategy(local.options, local.authenticate))
|
|
|
|
passport.use(new JwtStrategy(jwt.options, jwt.authenticate))
|
|
|
|
// passport.use(new GoogleStrategy(google.options, google.authenticate))
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => done(null, user))
|
|
|
|
|
|
|
|
passport.deserializeUser(async (user, done) => {
|
2021-04-20 18:17:44 +02:00
|
|
|
const db = getDB(StaticDatabases.GLOBAL.name)
|
2021-04-07 12:33:16 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await db.get(user._id)
|
|
|
|
return done(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("User not found", err)
|
|
|
|
return done(null, false, { message: "User not found" })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
module.exports = {
|
2021-04-15 17:45:21 +02:00
|
|
|
init(pouch) {
|
2021-04-20 18:17:44 +02:00
|
|
|
setDB(pouch)
|
2021-04-15 17:45:21 +02:00
|
|
|
},
|
2021-04-21 17:42:44 +02:00
|
|
|
db: require("./db/utils"),
|
|
|
|
utils: {
|
|
|
|
...require("./utils"),
|
|
|
|
...require("./hashing")
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
buildAuthMiddleware: authenticated,
|
|
|
|
passport,
|
|
|
|
},
|
|
|
|
constants: require("./constants"),
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|