From 0f002973d7c2bf6c59b18e7a5bedbda94c288cc0 Mon Sep 17 00:00:00 2001 From: Kilian Date: Mon, 8 Oct 2018 15:56:25 +0200 Subject: [PATCH] fix: Merge with other changes from 2.0 overhaul, specifically cookie logic. --- index.js | 14 -------------- vue/app-server.js | 7 ++++++- vue/app.js | 17 ++++++++++++----- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index e9a271a..901f212 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,6 @@ const API = require('cubic-api') const local = require('./config/local.js') const WebpackServer = require('./controllers/webpack.js') const endpoints = require('./override/endpoints.js') -const Cookies = require('cookies') class Ui { constructor (options) { @@ -20,19 +19,6 @@ class Ui { await cubic.use(new API(cubic.config.ui.api)) await cubic.use(new Core(cubic.config.ui.core)) - // Attach access token from cookie to req - if (!cubic.config.ui.api.disable) { - cubic.nodes.ui.api.server.http.app.use((req, res, next) => { - const cookies = new Cookies(req, res) - const token = cookies.get(cubic.config.ui.client.accessTokenCookie) - if (token && !req.headers.authorization) { - req.access_token = token - req.headers.authorization = `bearer ${token}` - } - next() - }) - } - // Implicitly load sites as endpoints, start webpack bundler if required. if (!cubic.config.ui.core.disable) { const client = cubic.nodes.ui.core.client diff --git a/vue/app-server.js b/vue/app-server.js index 96b2629..b8f9a6d 100644 --- a/vue/app-server.js +++ b/vue/app-server.js @@ -10,7 +10,12 @@ export default function (context) { return new Promise((resolve, reject) => { const { app, router, store } = createApp(context) - if (context.req.access_token) app.$cubic.setAccessToken(context.req.access_token) + // TODO: Make this work. Right now there's no way it would as it overlaps + // with other requests. My suggestion is to fall back to HTTP requests + // for asyncData hooks on the server. Otherwise we'd have to create + // a new cubic-client instance on every request (terrible memory footprint) + // + // if (context.req.access_token) app.$cubic.setAccessToken(context.req.access_token) // Init vue-meta const meta = app.$meta() diff --git a/vue/app.js b/vue/app.js index 4d56480..bb580db 100644 --- a/vue/app.js +++ b/vue/app.js @@ -15,12 +15,19 @@ import { sync } from 'vuex-router-sync' // export a factory function for creating fresh app, router and store // instances export function createApp (context) { - /* eslint no-undef: "off" */ - Vue.prototype.$cubic = new Client({ - api_url: $apiUrl, - auth_url: $authUrl - }) + // We're on the server -> Get pre-connected api connection from node + if (context) { + Vue.prototype.$cubic = context.api + } + // We're on the client -> load API connection + else { + /* eslint no-undef: "off" */ + Vue.prototype.$cubic = new Client({ + api_url: $apiUrl, + auth_url: $authUrl + }) + } const router = createRouter() const store = createStore(Vue.prototype.$cubic)