Skip to content

Commit

Permalink
Added missing auth token (#456)
Browse files Browse the repository at this point in the history
* Added missing auth token
* Added auth token to route request to make it work
  • Loading branch information
tim-yao authored Jul 26, 2019
1 parent 4fd615a commit 82d0635
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/ripple-nuxt-tide/lib/core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function (context, results) {
const section = context.route.query.section ? context.route.query.section : null
response = await context.app.$tide.getPreviewPage(type, id, rev, section, tideParams, authToken)
} else {
response = await context.app.$tide.getPageByPath(context.route.fullPath, tideParams)
response = await context.app.$tide.getPageByPath(context.route.fullPath, tideParams, authToken)
}

// If redirect required.
Expand Down
44 changes: 21 additions & 23 deletions packages/ripple-nuxt-tide/lib/core/tide.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@ export const tide = (axios, site, config) => ({
* @param {String} authToken Authentication token
*/
get: async function (resource, params = {}, id = '', authToken) {
const siteParam = 'site=' + site
const url = `${apiPrefix}${resource}${id ? `/${id}` : ''}?${siteParam}${Object.keys(params).length ? `&${qs.stringify(params, { indices: false })}` : ''}`
let headers = {}

if (process.server || process.env.NODE_ENV === 'development') {
console.info(`Tide request url: ${url}`)
// axios config
const axiosConfig = {
baseUrl: config.baseUrl,
auth: config.auth,
headers: {}
}

if (this.isModuleEnabled('authenticatedContent')) {
// Set 'X-Authorization' header if authToken present
if (authToken) {
if (!isTokenExpired(authToken)) {
_.merge(headers, { 'X-Authorization': `Bearer ${authToken}` })
} else {
delete config.headers['X-Authorization']
}
} else if (config.headers && config.headers['X-Authorization']) {
delete config.headers['X-Authorization']
if (authToken && !isTokenExpired(authToken)) {
_.merge(axiosConfig.headers, { 'X-Authorization': `Bearer ${authToken}` })
}
}

// If headers is not empty add to config request
if (!_.isEmpty(headers)) {
_.merge(config, { headers: headers })
const siteParam = 'site=' + site
const url = `${apiPrefix}${resource}${id ? `/${id}` : ''}?${siteParam}${Object.keys(params).length ? `&${qs.stringify(params, { indices: false })}` : ''}`

if (process.server || process.env.NODE_ENV === 'development') {
console.info(`Tide request url: ${url}`)
}
return axios.$get(url, config)
return axios.$get(url, axiosConfig)
},

post: async function (resource, data = {}, id = '') {
Expand Down Expand Up @@ -229,13 +224,13 @@ export const tide = (axios, site, config) => ({
}
},

getPathData: async function (path, params) {
getPathData: async function (path, params, authToken) {
let routeParams = { path: path }
if (!_.isEmpty(params)) {
_.merge(routeParams, params)
}

const response = await this.get('route', routeParams)
const response = await this.get('route', routeParams, '', authToken)
return response
},

Expand Down Expand Up @@ -299,9 +294,9 @@ export const tide = (axios, site, config) => ({
return entity
},

getPageByPath: async function (path, params) {
getPageByPath: async function (path, params, authToken) {
let pageData = null
const response = await this.getPathData(path, params)
const response = await this.getPathData(path, params, authToken)

const pathData = jsonapiParse.parse(response).data

Expand All @@ -310,7 +305,10 @@ export const tide = (axios, site, config) => ({
return pathData
}

const entity = await this.getEntityByPathData(pathData, params)
const entity = await this.getEntityByPathData(pathData, params, authToken)
if (!entity) {
throw new Error('Something wrong. Could not get any entity data from Tide based on API route response.')
}
pageData = jsonapiParse.parse(entity).data

// Append the site section to page data
Expand Down
16 changes: 8 additions & 8 deletions packages/ripple-nuxt-tide/lib/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export default ({ env, app, req, res, store , route}, inject) => {
const routeRequest = responseUrl.includes('/route?')
const authPreviewRequest = responseUrl.includes('&current_version=') || responseUrl.includes('&resourceVersion=')

// Set http status code if a route or preview request failed.
if (routeRequest || authPreviewRequest) {
let code
if (error.code) {
code = error.code
} else if (error.response) {
code = error.response.status
}
let code
if (error.code) {
code = error.code
} else if (error.response) {
code = error.response.status
}

// Set http status code if a route or preview request failed.
if (routeRequest || authPreviewRequest || code === 404) {
// We hide 403 and show it as 404
code = code === 403 ? 404 : code

Expand Down

0 comments on commit 82d0635

Please sign in to comment.