Skip to content

Commit

Permalink
feat: add authorization header to query
Browse files Browse the repository at this point in the history
  • Loading branch information
SplitCode committed Oct 4, 2024
1 parent f1de48c commit fd50c13
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import prepareHeaders from '../helpers/prepareHeaders';
import apiPath from './apiPath';

export const channelsApi = createApi({
reducerPath: 'channels',
baseQuery: fetchBaseQuery({ baseUrl: apiPath.channels() }),
baseQuery: fetchBaseQuery({ baseUrl: apiPath.channels(), prepareHeaders }),
endpoints: (builder) => ({
getChannels: builder.query({
query: () => '',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Channels.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Nav from 'react-bootstrap/Nav';
import Col from 'react-bootstrap/Col';
import Button from 'react-bootstrap/Button';
import { Plus } from 'react-bootstrap-icons';
import { useGetChannelsQuery } from '../api/getChannels';
import { useGetChannelsQuery } from '../api/channelsApi';

const Channels = () => {
const channels = useGetChannelsQuery;
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/helpers/prepareHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const prepareHeaders = (headers, { getState }) => {
const state = getState();
const { token } = state.auth;

if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
return headers;
};

export default prepareHeaders;
2 changes: 1 addition & 1 deletion frontend/src/store/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { configureStore } from '@reduxjs/toolkit';
import { channelsApi } from '../api/getChannels';
import { channelsApi } from '../api/channelsApi';

import authReducer from './slices/authSlice';

Expand Down

0 comments on commit fd50c13

Please sign in to comment.