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
|
|
|
|
const CouchDB = require("./db")
|
|
|
|
const { StaticDatabases } = require("./db/utils")
|
2021-04-11 12:35:55 +02:00
|
|
|
const { jwt, local, google, authenticated } = require("./middleware")
|
2021-04-08 12:20:37 +02:00
|
|
|
const { Cookies, UserStatus } = require("./constants")
|
|
|
|
const { hash, compare } = require("./hashing")
|
2021-04-12 19:31:58 +02:00
|
|
|
const { getAppId, setCookie, getCookie } = require("./utils")
|
2021-04-11 12:35:55 +02:00
|
|
|
const {
|
|
|
|
generateUserID,
|
|
|
|
getUserParams,
|
|
|
|
getEmailFromUserID,
|
|
|
|
} = require("./db/utils")
|
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) => {
|
|
|
|
const db = new CouchDB(StaticDatabases.USER.name)
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
passport,
|
|
|
|
Cookies,
|
|
|
|
UserStatus,
|
2021-04-08 12:26:08 +02:00
|
|
|
StaticDatabases,
|
|
|
|
generateUserID,
|
|
|
|
getUserParams,
|
2021-04-08 17:58:33 +02:00
|
|
|
getEmailFromUserID,
|
2021-04-08 12:20:37 +02:00
|
|
|
hash,
|
|
|
|
compare,
|
|
|
|
getAppId,
|
|
|
|
setCookie,
|
2021-04-12 19:31:58 +02:00
|
|
|
getCookie,
|
2021-04-11 12:35:55 +02:00
|
|
|
authenticated,
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|