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 all 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
23 changes: 23 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,31 @@ const cache = cacheExchange({
},
});

export const API_AUTH_KEY = "the-zoo.api.token";

function getToken(): string {
const value = localStorage.getItem(API_AUTH_KEY);
if (!value) return "";
const token = JSON.parse(value)["token"];
return token ? token : "";
}

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

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

return {};
}
});

/**
Expand Down
1 change: 0 additions & 1 deletion frontend/plugins/services/src/state/AppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type { SettingsState };
export const AppContext = React.createContext<[State, Dispatch<Action>]>(
[] as any,
);
export const STORAGE_KEY = "the-zoo.api.token";

const initialState: State = {
token: '',
Expand Down
9 changes: 5 additions & 4 deletions frontend/plugins/services/src/state/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { errorApiRef, useApi } from '@backstage/core';
import { useContext, useEffect } from 'react';
import { AppContext, STORAGE_KEY } from './AppState';
imunitic marked this conversation as resolved.
Show resolved Hide resolved
import { API_AUTH_KEY } from 'zoo-api';
import { Settings } from './types';
import { AppContext } from './AppState'

export function useSettings() {
const [settings, dispatch] = useContext(AppContext);
Expand All @@ -11,8 +12,8 @@ export function useSettings() {
useEffect(() => {
const rehydrate = () => {
try {
const stateFromStorage = JSON.parse(
localStorage.getItem(STORAGE_KEY)!,
const stateFromStorage = JSON.parse(
localStorage.getItem(API_AUTH_KEY)!,
);
if (
stateFromStorage &&
Expand All @@ -33,7 +34,7 @@ export function useSettings() {
}, [dispatch, errorApi, settings]);

const persist = (state: Settings) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
localStorage.setItem(API_AUTH_KEY, JSON.stringify(state));
};

return [
Expand Down