Fix API calls using a bearer auth token

This commit is contained in:
2020-04-22 09:17:30 +02:00
parent 272773692f
commit 17feb712b9
6 changed files with 17 additions and 10 deletions

View File

@@ -30,7 +30,7 @@
<section class="page"> <section class="page">
<div class="wrap"> <div class="wrap">
<div class="page__top"> <div class="page__top">
<a href="/" class="button-control button-control--pink dir-left"> <a href="/" class="button-control button-control--pink dir-left" on:click|preventDefault={() => window.location = '/'}>
<IconArrow direction="left" color="#fff" class="icon" /> <IconArrow direction="left" color="#fff" class="icon" />
<IconArrow direction="left" color="#fff" class="icon" hidden="true" /> <IconArrow direction="left" color="#fff" class="icon" hidden="true" />
</a> </a>

View File

@@ -2,7 +2,10 @@
export async function preload (page, session) { export async function preload (page, session) {
const req = await this.fetch(apiEndpoints.gql, { const req = await this.fetch(apiEndpoints.gql, {
method: 'post', method: 'post',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json',
'Authorization': 'bearer ' + process.env.CONFIG.API_TOKEN
},
body: JSON.stringify({ query: `{ body: JSON.stringify({ query: `{
site { site {
data { data {

View File

@@ -4,15 +4,16 @@
// Preload data (photos to display) // Preload data (photos to display)
export async function preload (page, session) { export async function preload (page, session) {
// Fields // Load Carousel photos
const fields = [ const fields = [
'id', 'name', 'image.private_hash', 'id', 'name', 'image.private_hash',
'location.id', 'location.name', 'location.slug', 'location.id', 'location.name', 'location.slug',
'location.country.name', 'location.country.slug' 'location.country.name', 'location.country.slug'
] ]
// Random sort const sort = '?' // Random sort
const sort = '?' const req = await this.fetch(`${apiEndpoints.rest}/items/photos?fields=${fields.join()}&sort=${sort}&limit=${limit}`, {
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() const photos = await req.json()
if (req.ok) { if (req.ok) {
return { photos: photos.data } return { photos: photos.data }

View File

@@ -12,7 +12,9 @@
].join(',') ].join(',')
const sort = ['-created_on', 'name'].join(',') const sort = ['-created_on', 'name'].join(',')
const limit = -1 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() const photos = await req.json()
if (req.ok) { if (req.ok) {
return { photos: photos.data } return { photos: photos.data }

View File

@@ -7,13 +7,14 @@
export async function preload (page, session) { export async function preload (page, session) {
// Load the photos if not loaded // Load the photos if not loaded
if (!preloaded) { if (!preloaded) {
// Fields
const fields = [ const fields = [
'id', 'name', 'slug', 'date', 'image.private_hash', 'id', 'name', 'slug', 'date', 'image.private_hash',
'location.id', 'location.name', 'location.slug', 'location.id', 'location.name', 'location.slug',
'location.country.name', 'location.country.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() const photos = await req.json()
if (req.ok) { if (req.ok) {
return { photos: photos.data } return { photos: photos.data }

View File

@@ -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 const apiEndpoint = dev ? process.env.CONFIG.API_URL_DEV : process.env.CONFIG.API_URL_PROD
export const apiEndpoints = { export const apiEndpoints = {
gql: `${apiEndpoint}/gql?access_token=${process.env.CONFIG.API_TOKEN}`, gql: apiEndpoint + '/gql',
rest: apiEndpoint rest: apiEndpoint
} }