Fixing issues with auth not being able to find appId in pathname, using the currentapp cookie if pathname not populated.

This commit is contained in:
mike12345567 2020-11-06 21:13:21 +00:00
parent b2d123b7fa
commit 472305d214
8 changed files with 28 additions and 20 deletions

View File

@ -1,12 +1,12 @@
import { authenticate } from "./authenticate" import { authenticate } from "./authenticate"
import { getAppIdFromPath } from "../render/getAppId" import { getAppId } from "../render/getAppId"
const apiCall = method => async ({ url, body }) => { const apiCall = method => async ({ url, body }) => {
const response = await fetch(url, { const response = await fetch(url, {
method: method, method: method,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-budibase-app-id": getAppIdFromPath(), "x-budibase-app-id": getAppId(window.document.cookie),
}, },
body: body && JSON.stringify(body), body: body && JSON.stringify(body),
credentials: "same-origin", credentials: "same-origin",

View File

@ -2,7 +2,7 @@ import { attachChildren } from "./render/attachChildren"
import { createTreeNode } from "./render/prepareRenderComponent" import { createTreeNode } from "./render/prepareRenderComponent"
import { screenRouter } from "./render/screenRouter" import { screenRouter } from "./render/screenRouter"
import { createStateManager } from "./state/stateManager" import { createStateManager } from "./state/stateManager"
import { getAppIdFromPath } from "./render/getAppId" import { getAppId } from "./render/getAppId"
export const createApp = ({ export const createApp = ({
componentLibraries, componentLibraries,
@ -38,7 +38,7 @@ export const createApp = ({
window, window,
}) })
const fallbackPath = window.location.pathname.replace( const fallbackPath = window.location.pathname.replace(
getAppIdFromPath(), getAppId(window.document.cookie),
"" ""
) )
routeTo(currentUrl || fallbackPath) routeTo(currentUrl || fallbackPath)

View File

@ -1,6 +1,6 @@
import { createApp } from "./createApp" import { createApp } from "./createApp"
import { builtins, builtinLibName } from "./render/builtinComponents" import { builtins, builtinLibName } from "./render/builtinComponents"
import { getAppIdFromPath } from "./render/getAppId" import { getAppId } from "./render/getAppId"
/** /**
* create a web application from static budibase definition files. * create a web application from static budibase definition files.
@ -9,7 +9,7 @@ import { getAppIdFromPath } from "./render/getAppId"
export const loadBudibase = async opts => { export const loadBudibase = async opts => {
const _window = (opts && opts.window) || window const _window = (opts && opts.window) || window
// const _localStorage = (opts && opts.localStorage) || localStorage // const _localStorage = (opts && opts.localStorage) || localStorage
const appId = getAppIdFromPath() const appId = getAppId(window.document.cookie)
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"] const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
const user = {} const user = {}

View File

@ -1,4 +1,15 @@
export const getAppIdFromPath = () => { const COOKIE_SEPARATOR = ";"
const APP_PREFIX = "app_"
const KEY_VALUE_SPLIT = "="
export const getAppId = (allCookies) => {
const cookie = allCookies.split(COOKIE_SEPARATOR).find(cookie => cookie.trim().startsWith("budibase:currentapp"))
let appId = location.pathname.split("/")[1] let appId = location.pathname.split("/")[1]
return appId && appId.startsWith("app_") ? appId : undefined appId = appId && appId.startsWith(APP_PREFIX) ? appId : undefined
if (!appId && cookie && cookie.split(KEY_VALUE_SPLIT).length === 2) {
appId = cookie.split("=")[1]
}
return appId
} }
export const getAppIdFromPath = getAppId

View File

@ -1,6 +1,6 @@
import regexparam from "regexparam" import regexparam from "regexparam"
import appStore from "../state/store" import appStore from "../state/store"
import { getAppIdFromPath } from "./getAppId" import { getAppId } from "./getAppId"
export const screenRouter = ({ screens, onScreenSelected, window }) => { export const screenRouter = ({ screens, onScreenSelected, window }) => {
function sanitize(url) { function sanitize(url) {
@ -27,7 +27,7 @@ export const screenRouter = ({ screens, onScreenSelected, window }) => {
const makeRootedPath = url => { const makeRootedPath = url => {
if (isRunningLocally()) { if (isRunningLocally()) {
const appId = getAppIdFromPath() const appId = getAppId(window.document.cookie)
if (url) { if (url) {
url = sanitize(url) url = sanitize(url)
if (!url.startsWith("/")) { if (!url.startsWith("/")) {

View File

@ -1,7 +1,7 @@
import { load, makePage, makeScreen, walkComponentTree } from "./testAppDef" import { load, makePage, makeScreen, walkComponentTree } from "./testAppDef"
import { isScreenSlot } from "../src/render/builtinComponents" import { isScreenSlot } from "../src/render/builtinComponents"
jest.mock("../src/render/getAppId", () => ({ jest.mock("../src/render/getAppId", () => ({
getAppIdFromPath: () => "TEST_APP_ID" getAppId: () => "TEST_APP_ID"
})) }))
describe("screenRouting", () => { describe("screenRouting", () => {

View File

@ -6,15 +6,12 @@ const {
} = require("../../utilities/budibaseDir") } = require("../../utilities/budibaseDir")
exports.fetchAppComponentDefinitions = async function(ctx) { exports.fetchAppComponentDefinitions = async function(ctx) {
const db = new CouchDB(ctx.params.appId) const appId = ctx.params.appId || ctx.appId
const app = await db.get(ctx.params.appId) const db = new CouchDB(appId)
const app = await db.get(appId)
ctx.body = app.componentLibraries.reduce((acc, componentLibrary) => { ctx.body = app.componentLibraries.reduce((acc, componentLibrary) => {
let appDirectory = resolve( let appDirectory = resolve(budibaseAppsDir(), appId, "node_modules")
budibaseAppsDir(),
ctx.params.appId,
"node_modules"
)
if (ctx.isDev) { if (ctx.isDev) {
appDirectory = budibaseTempDir() appDirectory = budibaseTempDir()

View File

@ -197,10 +197,11 @@ exports.serveAppAsset = async function(ctx) {
} }
exports.serveComponentLibrary = async function(ctx) { exports.serveComponentLibrary = async function(ctx) {
const appId = ctx.query.appId || ctx.appId
// default to homedir // default to homedir
let componentLibraryPath = resolve( let componentLibraryPath = resolve(
budibaseAppsDir(), budibaseAppsDir(),
ctx.query.appId, appId,
"node_modules", "node_modules",
decodeURI(ctx.query.library), decodeURI(ctx.query.library),
"package", "package",
@ -222,7 +223,6 @@ exports.serveComponentLibrary = async function(ctx) {
} else { } else {
componentLib += `-${COMP_LIB_BASE_APP_VERSION}` componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
} }
const appId = ctx.query.appId
const S3_URL = encodeURI( const S3_URL = encodeURI(
`https://${appId}.app.budi.live/assets/${componentLib}/${ctx.query.library}/dist/index.js` `https://${appId}.app.budi.live/assets/${componentLib}/${ctx.query.library}/dist/index.js`
) )