From 17feb712b95a16297fffd4732c6b21fe9b9f8972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Wed, 22 Apr 2020 09:17:30 +0200 Subject: [PATCH] Fix API calls using a bearer auth token --- src/routes/_error.svelte | 2 +- src/routes/_layout.svelte | 5 ++++- src/routes/index.svelte | 9 +++++---- src/routes/location/[country]/[place].svelte | 4 +++- src/routes/viewer/[country]/[place]/[photo].svelte | 5 +++-- src/utils/store.js | 2 +- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/routes/_error.svelte b/src/routes/_error.svelte index 3394f33..aa3ab12 100644 --- a/src/routes/_error.svelte +++ b/src/routes/_error.svelte @@ -30,7 +30,7 @@
- + window.location = '/'}> diff --git a/src/routes/_layout.svelte b/src/routes/_layout.svelte index ec91b2b..b6fd360 100644 --- a/src/routes/_layout.svelte +++ b/src/routes/_layout.svelte @@ -2,7 +2,10 @@ export async function preload (page, session) { const req = await this.fetch(apiEndpoints.gql, { method: 'post', - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'bearer ' + process.env.CONFIG.API_TOKEN + }, body: JSON.stringify({ query: `{ site { data { diff --git a/src/routes/index.svelte b/src/routes/index.svelte index 317aeaa..242cc41 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -4,15 +4,16 @@ // Preload data (photos to display) export async function preload (page, session) { - // Fields + // Load Carousel photos const fields = [ 'id', 'name', 'image.private_hash', 'location.id', 'location.name', 'location.slug', 'location.country.name', 'location.country.slug' ] - // Random sort - const sort = '?' - const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields.join()}&sort=${sort}&limit=${limit}`) + const sort = '?' // Random sort + const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields.join()}&sort=${sort}&limit=${limit}`, { + 'Authorization': 'bearer ' + process.env.CONFIG.API_TOKEN + }) const photos = await req.json() if (req.ok) { return { photos: photos.data } diff --git a/src/routes/location/[country]/[place].svelte b/src/routes/location/[country]/[place].svelte index 52efe60..f48688d 100644 --- a/src/routes/location/[country]/[place].svelte +++ b/src/routes/location/[country]/[place].svelte @@ -12,7 +12,9 @@ ].join(',') const sort = ['-created_on', 'name'].join(',') const limit = -1 - const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields}&filter[location.slug][rlike]=%${page.params.place}%&limit=${limit}&sort=${sort}`) + const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields}&filter[location.slug][rlike]=%${page.params.place}%&limit=${limit}&sort=${sort}`, { + 'Authorization': 'bearer ' + process.env.CONFIG.API_TOKEN + }) const photos = await req.json() if (req.ok) { return { photos: photos.data } diff --git a/src/routes/viewer/[country]/[place]/[photo].svelte b/src/routes/viewer/[country]/[place]/[photo].svelte index 434f759..f60cf2d 100644 --- a/src/routes/viewer/[country]/[place]/[photo].svelte +++ b/src/routes/viewer/[country]/[place]/[photo].svelte @@ -7,13 +7,14 @@ export async function preload (page, session) { // Load the photos if not loaded if (!preloaded) { - // Fields const fields = [ 'id', 'name', 'slug', 'date', 'image.private_hash', 'location.id', 'location.name', 'location.slug', 'location.country.name', 'location.country.slug' ] - const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields.join()}&filter[location.slug][rlike]=%${page.params.place}%`) + const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields.join()}&filter[location.slug][rlike]=%${page.params.place}%`, { + 'Authorization': 'bearer ' + process.env.CONFIG.API_TOKEN + }) const photos = await req.json() if (req.ok) { return { photos: photos.data } diff --git a/src/utils/store.js b/src/utils/store.js index 81312a6..a5daba7 100644 --- a/src/utils/store.js +++ b/src/utils/store.js @@ -9,7 +9,7 @@ export const dev = process.env.NODE_ENV === 'development' ========================================================================== */ const apiEndpoint = dev ? process.env.CONFIG.API_URL_DEV : process.env.CONFIG.API_URL_PROD export const apiEndpoints = { - gql: `${apiEndpoint}/gql?access_token=${process.env.CONFIG.API_TOKEN}`, + gql: apiEndpoint + '/gql', rest: apiEndpoint }