Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

Commit

Permalink
fix: Merge with other changes from 2.0 overhaul, specifically cookie …
Browse files Browse the repository at this point in the history
…logic.
  • Loading branch information
Kaptard committed Oct 8, 2018
1 parent 5cdd464 commit 0f00297
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
14 changes: 0 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion vue/app-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 12 additions & 5 deletions vue/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0f00297

Please sign in to comment.