diff --git a/.env b/.env index de6de54..40ec8e3 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ VITE_BACKEND_URL= +VITE_DOOR_BACKEND_URL= VITE_MQTT_URL= VITE_OAUTH_CLIENT_ID= diff --git a/.env.development b/.env.development index d959023..c4e56ca 100644 --- a/.env.development +++ b/.env.development @@ -1,3 +1,4 @@ VITE_BACKEND_URL=http://localhost:3000/ +VITE_DOOR_BACKEND_URL=http://localhost:3000/ VITE_MQTT_URL=ws://localhost:1083/mqtt VITE_OAUTH_CLIENT_ID=hCOEcK3ntyBym-uLQkogX6w8457kicVlZbY0PQZJusw diff --git a/.env.production b/.env.production index 523e6c4..b1266a1 100644 --- a/.env.production +++ b/.env.production @@ -1,3 +1,4 @@ VITE_BACKEND_URL=https://fauna.initlab.org/ +VITE_DOOR_BACKEND_URL=https://fauna.initlab.org/ VITE_MQTT_URL=wss://spitfire.initlab.org:8083/mqtt VITE_OAUTH_CLIENT_ID=YueB6ct6SKPN8Ar72G0LC1QFxW9meUDQIOHdAu5mfCE diff --git a/src/app/store.js b/src/app/store.js index d782f76..31ed908 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -1,6 +1,6 @@ import { configureStore } from '@reduxjs/toolkit'; import { setupListeners } from '@reduxjs/toolkit/query'; -import { anonymousApiSlice, authenticatedApiSlice } from '../features/apiSlice'; +import { anonymousApiSlice, authenticatedApiSlice, authenticatedDoorApiSlice } from '../features/apiSlice'; import { sensorSlice } from '../features/sensorSlice'; import { doorSlice } from '../features/doorSlice.js'; @@ -8,11 +8,13 @@ export const store = configureStore({ reducer: { [anonymousApiSlice.reducerPath]: anonymousApiSlice.reducer, [authenticatedApiSlice.reducerPath]: authenticatedApiSlice.reducer, + [authenticatedDoorApiSlice.reducerPath]: authenticatedDoorApiSlice.reducer, [sensorSlice.name]: sensorSlice.reducer, [doorSlice.name]: doorSlice.reducer, }, middleware: getDefaultMiddleware => - getDefaultMiddleware().concat(anonymousApiSlice.middleware).concat(authenticatedApiSlice.middleware), + getDefaultMiddleware().concat(anonymousApiSlice.middleware).concat(authenticatedApiSlice.middleware) + .concat(authenticatedDoorApiSlice.middleware), }); setupListeners(store.dispatch); diff --git a/src/features/apiSlice.js b/src/features/apiSlice.js index 62bad90..01d8614 100644 --- a/src/features/apiSlice.js +++ b/src/features/apiSlice.js @@ -4,6 +4,7 @@ import { E_ALREADY_LOCKED, Mutex, tryAcquire } from 'async-mutex'; const refreshMutex = new Mutex(); const apiBaseUrl = import.meta.env.VITE_BACKEND_URL + 'api/'; +const doorApiBaseUrl = import.meta.env.VITE_DOOR_BACKEND_URL + 'api/'; const refreshToken = async (api, extraOptions) => { try { @@ -70,6 +71,21 @@ const authenticatedBaseQuery = fetchBaseQuery({ }, }); +const authenticatedDoorBaseQuery = fetchBaseQuery({ + baseUrl: doorApiBaseUrl, + prepareHeaders: headers => { + headers.set('accept', 'application/json'); + + const token = getToken(); + + if (token) { + headers.set('authorization', 'Bearer ' + token); + } + + return headers; + }, +}); + const authenticatedBaseQueryWithReauth = async (args, api, extraOptions) => { const expire = getTokenExpireTimestamp(); @@ -103,6 +119,13 @@ export const authenticatedApiSlice = createApi({ baseQuery: authenticatedBaseQueryWithReauth, endpoints: builder => ({ getCurrentUser: query(builder)('current_user'), + }), +}); + +export const authenticatedDoorApiSlice = createApi({ + reducerPath: 'authenticatedDoorApi', + baseQuery: authenticatedDoorBaseQuery, + endpoints: builder => ({ getDoors: query(builder)('doors'), doorAction: builder.mutation({ query: params => ({ @@ -119,6 +142,9 @@ export const { export const { useGetCurrentUserQuery, +} = authenticatedApiSlice; + +export const { useGetDoorsQuery, useDoorActionMutation, -} = authenticatedApiSlice; +} = authenticatedDoorApiSlice;