Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

feat: add useApiTokenAuth config #362

Open
wants to merge 7 commits into
base: hangar
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/app-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
app:
title: Hangar App
baseUrl: http://localhost:3000
useApiTokenAuth: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work for you? It seems like we’d need to change the schema of the config somewhere.
Anyway, it doesn’t pick the config when I’m trying locally 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have to successfully run this locally.


organization:
name: Kiwi.com
Expand Down
22 changes: 22 additions & 0 deletions frontend/packages/zoo-api/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client, dedupExchange, fetchExchange } from 'urql';
import { cacheExchange } from '@urql/exchange-graphcache';
import { relayPagination } from '@urql/exchange-graphcache/extras';
import { Connection, Edge, Node } from './queries';
import { useApi, configApiRef } from '@backstage/core';

const cache = cacheExchange({
resolvers: {
Expand All @@ -11,9 +12,30 @@ const cache = cacheExchange({
},
});


function getToken(): string {
const value = localeStorage.getItem("the-zoo.api.token");
imunitic marked this conversation as resolved.
Show resolved Hide resolved
if (!value) return "";
const token = JSON.parse(value)["token"];
return token ? token : "";
}

export const theZooClient = new Client({
url: '/graphql',
exchanges: [cache, dedupExchange, fetchExchange],
fetchOptions: () => {
let config = useApi(configApiRef);
let useApiTokenAuth = config.getBoolean("app.useApiTokenAuth");

if (useApiTokenAuth) {
const token: string = getToken();
return {
headers: { Authorization: token ? `Bearer ${token}` : '' },
};
}

return {};
}
});

/**
Expand Down