1
0
Fork 0
mirror of synced 2024-07-03 13:30:46 +12:00
budibase/packages/auth/src/index.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-04-07 22:33:16 +12: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-16 03:57:01 +12:00
const database = require("./db")
2021-04-07 22:33:16 +12:00
const { StaticDatabases } = require("./db/utils")
2021-04-15 03:44:10 +12:00
const { jwt, local, authenticated } = require("./middleware")
const { Cookies, UserStatus } = require("./constants")
const { hash, compare } = require("./hashing")
2021-04-15 03:01:28 +12:00
const {
getAppId,
setCookie,
getCookie,
clearCookie,
isClient,
} = require("./utils")
2021-04-11 22:35:55 +12:00
const {
generateUserID,
getUserParams,
2021-04-19 22:34:07 +12:00
generateGroupID,
getGroupParams,
2021-04-11 22:35:55 +12:00
} = require("./db/utils")
const { getGlobalUserByEmail } = require("./utils")
2021-04-07 22:33:16 +12: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 database.CouchDB(StaticDatabases.GLOBAL.name)
2021-04-07 22:33:16 +12: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" })
}
})
module.exports = {
init(pouch) {
2021-04-16 03:57:01 +12:00
database.setDB(pouch)
},
passport,
Cookies,
UserStatus,
2021-04-08 22:26:08 +12:00
StaticDatabases,
generateUserID,
getUserParams,
2021-04-20 03:16:46 +12:00
generateGroupID,
getGroupParams,
hash,
compare,
getAppId,
setCookie,
getCookie,
2021-04-14 00:56:28 +12:00
clearCookie,
2021-04-11 22:35:55 +12:00
authenticated,
isClient,
getGlobalUserByEmail,
}