Skip to content

Commit

Permalink
Split Door API from OAuth2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
user890104 committed Aug 27, 2023
1 parent 967f29e commit 0cc4868
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
VITE_BACKEND_URL=
VITE_DOOR_BACKEND_URL=
VITE_MQTT_URL=
VITE_OAUTH_CLIENT_ID=
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions src/app/store.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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';

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);
28 changes: 27 additions & 1 deletion src/features/apiSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 => ({
Expand All @@ -119,6 +142,9 @@ export const {

export const {
useGetCurrentUserQuery,
} = authenticatedApiSlice;

export const {
useGetDoorsQuery,
useDoorActionMutation,
} = authenticatedApiSlice;
} = authenticatedDoorApiSlice;

0 comments on commit 0cc4868

Please sign in to comment.