2021-04-11 22:35:55 +12:00
|
|
|
const { passport, Cookies } = require("@budibase/auth")
|
2021-04-02 08:34:43 +13:00
|
|
|
|
|
|
|
exports.authenticate = async (ctx, next) => {
|
2021-04-08 02:15:05 +12:00
|
|
|
return passport.authenticate("local", async (err, user) => {
|
2021-04-07 22:33:16 +12:00
|
|
|
if (err) {
|
2021-04-08 02:15:05 +12:00
|
|
|
return ctx.throw(err)
|
2021-04-07 22:33:16 +12:00
|
|
|
}
|
|
|
|
|
2021-04-08 02:15:05 +12:00
|
|
|
const expires = new Date()
|
|
|
|
expires.setDate(expires.getDate() + 1)
|
|
|
|
|
2021-04-12 21:47:48 +12:00
|
|
|
if (!user) {
|
|
|
|
ctx.body = { success: false, user }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-11 22:35:55 +12:00
|
|
|
ctx.cookies.set(Cookies.Auth, user.token, {
|
2021-04-08 02:15:05 +12:00
|
|
|
expires,
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
overwrite: true,
|
|
|
|
})
|
|
|
|
|
2021-04-12 21:47:48 +12:00
|
|
|
delete user.token
|
|
|
|
|
|
|
|
ctx.body = { success: true, user }
|
2021-04-02 08:34:43 +13:00
|
|
|
})(ctx, next)
|
|
|
|
}
|
2021-04-11 22:35:55 +12:00
|
|
|
|
|
|
|
exports.googleAuth = async (ctx, next) => {
|
|
|
|
// return passport.authenticate("google")
|
|
|
|
}
|