Authentication working on builder homepage, integration with currentapp middleware

This commit is contained in:
Martin McKeaveney 2021-04-13 11:56:57 +01:00
parent a52f296d78
commit 3226ee90e2
11 changed files with 33 additions and 17 deletions

View File

@ -11,7 +11,7 @@ module.exports = async (ctx, next) => {
ctx.isAuthenticated = true ctx.isAuthenticated = true
ctx.user = authCookie ctx.user = authCookie
// make sure email is correct from ID // make sure email is correct from ID
ctx.user.email = getEmailFromUserID(authCookie._id) ctx.user.email = getEmailFromUserID(authCookie.userId)
} }
await next() await next()

View File

@ -1,5 +1,10 @@
const { Cookies } = require("../../constants")
exports.options = { exports.options = {
secretOrKey: process.env.JWT_SECRET, secretOrKey: process.env.JWT_SECRET,
jwtFromRequest: function(ctx) {
return ctx.cookies.get(Cookies.Auth)
},
} }
exports.authenticate = async function(jwt, done) { exports.authenticate = async function(jwt, done) {

View File

@ -38,7 +38,7 @@ exports.authenticate = async function(username, password, done) {
// authenticate // authenticate
if (await compare(password, dbUser.password)) { if (await compare(password, dbUser.password)) {
const payload = { const payload = {
_id: dbUser._id, userId: dbUser._id,
} }
const token = jwt.sign(payload, process.env.JWT_SECRET, { const token = jwt.sign(payload, process.env.JWT_SECRET, {

View File

@ -12,7 +12,11 @@
username, username,
password, password,
}) })
notifier.success("Logged in successfully.") if (json.success) {
notifier.success("Logged in successfully.")
} else {
notifier.danger("Invalid credentials")
}
} catch (err) { } catch (err) {
console.error(err) console.error(err)
notifier.danger(`Error logging in: ${err}`) notifier.danger(`Error logging in: ${err}`)

View File

@ -3,8 +3,7 @@ import api from "../../builderStore/api"
async function checkAuth() { async function checkAuth() {
const response = await api.get("/api/self") const response = await api.get("/api/self")
const user = await response.json() return await response.json()
if (json) return json
} }
export function createAuthStore() { export function createAuthStore() {
@ -21,6 +20,7 @@ export function createAuthStore() {
localStorage.setItem("auth:user", JSON.stringify(json.user)) localStorage.setItem("auth:user", JSON.stringify(json.user))
set({ user: json.user }) set({ user: json.user })
} }
return json
}, },
logout: async () => { logout: async () => {
const response = await api.post(`/api/auth/logout`) const response = await api.post(`/api/auth/logout`)

View File

@ -145,7 +145,7 @@ exports.fetchAppPackage = async function(ctx) {
layouts, layouts,
clientLibPath: clientLibraryPath(ctx.params.appId), clientLibPath: clientLibraryPath(ctx.params.appId),
} }
await setBuilderToken(ctx, ctx.params.appId, application.version) // await setBuilderToken(ctx, ctx.params.appId, application.version)
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
@ -184,7 +184,7 @@ exports.create = async function(ctx) {
await createApp(appId) await createApp(appId)
} }
await setBuilderToken(ctx, appId, version) // await setBuilderToken(ctx, appId, version)
ctx.status = 200 ctx.status = 200
ctx.body = newApplication ctx.body = newApplication
ctx.message = `Application ${ctx.request.body.name} created successfully` ctx.message = `Application ${ctx.request.body.name} created successfully`

View File

@ -7,7 +7,7 @@ const { generateUserMetadataID } = require("../../db/utils")
const { setCookie } = require("../../utilities") const { setCookie } = require("../../utilities")
const { outputProcessing } = require("../../utilities/rowProcessor") const { outputProcessing } = require("../../utilities/rowProcessor")
const { InternalTables } = require("../../db/utils") const { InternalTables } = require("../../db/utils")
const { UserStatus } = require("@budibase/auth") const { UserStatus, StaticDatabases } = require("@budibase/auth")
const { getFullUser } = require("../../utilities/users") const { getFullUser } = require("../../utilities/users")
const INVALID_ERR = "Invalid Credentials" const INVALID_ERR = "Invalid Credentials"
@ -73,10 +73,19 @@ exports.authenticate = async ctx => {
exports.fetchSelf = async ctx => { exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user const { userId, appId } = ctx.user
/* istanbul ignore next */ /* istanbul ignore next */
if (!userId || !appId) { if (!userId) {
ctx.body = {} ctx.body = {}
return return
} }
if (!appId) {
const db = new CouchDB(StaticDatabases.USER.name)
const user = await db.get(userId)
delete user.password
ctx.body = { user }
return
}
const db = new CouchDB(appId) const db = new CouchDB(appId)
const user = await getFullUser({ ctx, userId: userId }) const user = await getFullUser({ ctx, userId: userId })
const userTable = await db.get(InternalTables.USER_METADATA) const userTable = await db.get(InternalTables.USER_METADATA)

View File

@ -9,7 +9,6 @@ const { processString } = require("@budibase/string-templates")
const { budibaseTempDir } = require("../../../utilities/budibaseDir") const { budibaseTempDir } = require("../../../utilities/budibaseDir")
const { getDeployedApps } = require("../../../utilities/builder/hosting") const { getDeployedApps } = require("../../../utilities/builder/hosting")
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const setBuilderToken = require("../../../utilities/builder/setBuilderToken")
const { const {
loadHandlebarsFile, loadHandlebarsFile,
NODE_MODULES_PATH, NODE_MODULES_PATH,
@ -35,9 +34,9 @@ const COMP_LIB_BASE_APP_VERSION = "0.2.5"
exports.serveBuilder = async function(ctx) { exports.serveBuilder = async function(ctx) {
let builderPath = resolve(TOP_LEVEL_PATH, "builder") let builderPath = resolve(TOP_LEVEL_PATH, "builder")
if (ctx.file === "index.html") { // if (ctx.file === "index.html") {
// await setBuilderToken(ctx) // // await setBuilderToken(ctx)
} // }
await send(ctx, ctx.file, { root: builderPath }) await send(ctx, ctx.file, { root: builderPath })
} }

View File

@ -4,7 +4,6 @@ const controller = require("../controllers/auth")
const router = Router() const router = Router()
// TODO: needs removed // TODO: needs removed
router.post("/api/authenticate", controller.authenticate)
router.get("/api/self", controller.fetchSelf) router.get("/api/self", controller.fetchSelf)
module.exports = router module.exports = router

View File

@ -15,7 +15,7 @@ function finish(ctx, next, { appId, roleId, cookie = false }) {
ctx.roleId = roleId ctx.roleId = roleId
} }
if (cookie && appId) { if (cookie && appId) {
setCookie(ctx, new CurrentAppCookie(appId, roleId)) setCookie(ctx, new CurrentAppCookie(appId, roleId), Cookies.CurrentApp)
} }
return next() return next()
} }

View File

@ -1,9 +1,9 @@
const CouchDB = require("../../db") const CouchDB = require("../db")
const { const {
generateUserMetadataID, generateUserMetadataID,
getEmailFromUserMetadataID, getEmailFromUserMetadataID,
} = require("../db/utils") } = require("../db/utils")
const { getGlobalUsers } = require("../../utilities/workerRequests") const { getGlobalUsers } = require("../utilities/workerRequests")
exports.getFullUser = async ({ ctx, email, userId }) => { exports.getFullUser = async ({ ctx, email, userId }) => {
if (!email) { if (!email) {