diff --git a/README.md b/README.md index 1aedc2b5..4d9e6da9 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,16 @@ If you're looking to develop or test Parseable Console, you can follow the steps 4. Run `pnpm dev` to start the console. 5. Open `http://localhost:3001` in your browser. +Enabling hot reload: + +1. Disable CORS on the server by setting `P_CORS=false`. +2. Configure the following environment variables: + ``` + VITE_USE_BASIC_AUTH=true + VITE_USERNAME=username + VITE_PASSWORD=password + ``` + To test production build 1. Run `pnpm build:test` to create a release build in test mode. diff --git a/src/api/axios.ts b/src/api/axios.ts index 5a07d9a0..fc983174 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,9 +1,34 @@ import { signOutHandler } from '@/utils'; import axios from 'axios'; +import _ from 'lodash'; +import Cookies from 'js-cookie'; const baseURL = import.meta.env.VITE_PARSEABLE_URL ?? '/'; +const useBasicAuth = import.meta.env.VITE_USE_BASIC_AUTH; -const instance = axios.create({ baseURL, withCredentials: true }); +const createAxiosInstance = () => { + const defaultOpts = { baseURL, withCredentials: true }; + const opts = (() => { + if (_.isString(useBasicAuth) && useBasicAuth === 'true') { + const username = import.meta.env.VITE_USERNAME; + const password = import.meta.env.VITE_PASSWORD; + if (!_.isString(username) || !_.isString(password)) return defaultOpts; + + // server will always send cookies + // creating mock cookies to avoid updating existing code since this implementation is focused on development only + Cookies.set('username', username); + Cookies.set('session', 'mock-key'); + const credentials = btoa(`${username}:${password}`); + const authHeader = `Basic ${credentials}`; + return { baseURL, withCredentials: false, headers: { Authorization: authHeader } }; + } else { + return defaultOpts; + } + })(); + return axios.create(opts); +}; + +const instance = createAxiosInstance(); instance.interceptors.request.use( (request) => { diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 2864218c..68302b18 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -2,6 +2,9 @@ interface ImportMetaEnv { readonly VITE_PARSEABLE_URL?: string; + readonly VITE_USE_BASIC_AUTH?: string; + readonly VITE_USERNAME?: string; + readonly VITE_PASSWORD?: string; } interface ImportMeta {