Skip to content

Commit

Permalink
Added auth headers to api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelmalak committed Nov 12, 2021
1 parent 0d36c5c commit d94a6ce
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 56 deletions.
15 changes: 10 additions & 5 deletions client/src/components/Apps/Apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ interface Props {
}

export const Apps = (props: Props): JSX.Element => {
const { apps, loading } = useSelector((state: State) => state.apps);
const {
apps: { apps, loading },
auth: { isAuthenticated },
} = useSelector((state: State) => state);

const dispatch = useDispatch();
const { getApps } = bindActionCreators(actionCreators, dispatch);
Expand Down Expand Up @@ -76,10 +79,12 @@ export const Apps = (props: Props): JSX.Element => {
subtitle={<Link to="/">Go back</Link>}
/>

<div className={classes.ActionsContainer}>
<ActionButton name="Add" icon="mdiPlusBox" handler={toggleModal} />
<ActionButton name="Edit" icon="mdiPencil" handler={toggleEdit} />
</div>
{isAuthenticated && (
<div className={classes.ActionsContainer}>
<ActionButton name="Add" icon="mdiPlusBox" handler={toggleModal} />
<ActionButton name="Edit" icon="mdiPencil" handler={toggleEdit} />
</div>
)}

<div className={classes.Apps}>
{loading ? (
Expand Down
53 changes: 28 additions & 25 deletions client/src/components/Bookmarks/Bookmarks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export enum ContentType {
}

export const Bookmarks = (props: Props): JSX.Element => {
const { loading, categories } = useSelector(
(state: State) => state.bookmarks
);
const {
bookmarks: { loading, categories },
auth: { isAuthenticated },
} = useSelector((state: State) => state);

const dispatch = useDispatch();
const { getCategories } = bindActionCreators(actionCreators, dispatch);
Expand Down Expand Up @@ -109,28 +110,30 @@ export const Bookmarks = (props: Props): JSX.Element => {

<Headline title="All Bookmarks" subtitle={<Link to="/">Go back</Link>} />

<div className={classes.ActionsContainer}>
<ActionButton
name="Add Category"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.category)}
/>
<ActionButton
name="Add Bookmark"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.bookmark)}
/>
<ActionButton
name="Edit Categories"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.category)}
/>
<ActionButton
name="Edit Bookmarks"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.bookmark)}
/>
</div>
{isAuthenticated && (
<div className={classes.ActionsContainer}>
<ActionButton
name="Add Category"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.category)}
/>
<ActionButton
name="Add Bookmark"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.bookmark)}
/>
<ActionButton
name="Edit Categories"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.category)}
/>
<ActionButton
name="Edit Bookmarks"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.bookmark)}
/>
</div>
)}

{loading ? (
<Spinner />
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export const Home = (): JSX.Element => {
<BookmarkGrid
categories={
!bookmarkSearchResult
? categories.filter(({ isPinned }) => isPinned)
? categories.filter(
({ isPinned, bookmarks }) => isPinned && bookmarks.length
)
: bookmarkSearchResult
}
totalCategories={categories.length}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const AuthForm = (): JSX.Element => {
id="password"
name="password"
placeholder="••••••"
autoComplete="current-password"
value={formData.password}
onChange={(e) =>
setFormData({ ...formData, password: e.target.value })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { actionCreators } from '../../../store';
// Typescript
import { ApiResponse } from '../../../interfaces';

// UI
// Other
import { InputGroup, Button } from '../../UI';
import { applyAuth } from '../../../utility';

export const StyleSettings = (): JSX.Element => {
const dispatch = useDispatch();
Expand All @@ -34,7 +35,11 @@ export const StyleSettings = (): JSX.Element => {
e.preventDefault();

axios
.put<ApiResponse<{}>>('/api/config/0/css', { styles: customStyles })
.put<ApiResponse<{}>>(
'/api/config/0/css',
{ styles: customStyles },
{ headers: applyAuth() }
)
.then(() => {
createNotification({
title: 'Success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import axios from 'axios';
import { useSelector } from 'react-redux';

// Typescript
import { Weather, ApiResponse, Config } from '../../../interfaces';
import { Weather, ApiResponse } from '../../../interfaces';

// CSS
import classes from './WeatherWidget.module.css';
Expand Down
34 changes: 26 additions & 8 deletions client/src/store/action-creators/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UpdateAppAction,
} from '../actions/app';
import axios from 'axios';
import { applyAuth } from '../../utility';

export const getApps =
() => async (dispatch: Dispatch<GetAppsAction<undefined | App[]>>) => {
Expand All @@ -20,7 +21,9 @@ export const getApps =
});

try {
const res = await axios.get<ApiResponse<App[]>>('/api/apps');
const res = await axios.get<ApiResponse<App[]>>('/api/apps', {
headers: applyAuth(),
});

dispatch({
type: ActionType.getAppsSuccess,
Expand All @@ -35,9 +38,15 @@ export const pinApp =
(app: App) => async (dispatch: Dispatch<PinAppAction>) => {
try {
const { id, isPinned, name } = app;
const res = await axios.put<ApiResponse<App>>(`/api/apps/${id}`, {
isPinned: !isPinned,
});
const res = await axios.put<ApiResponse<App>>(
`/api/apps/${id}`,
{
isPinned: !isPinned,
},
{
headers: applyAuth(),
}
);

const status = isPinned
? 'unpinned from Homescreen'
Expand All @@ -63,7 +72,9 @@ export const pinApp =
export const addApp =
(formData: NewApp | FormData) => async (dispatch: Dispatch<AddAppAction>) => {
try {
const res = await axios.post<ApiResponse<App>>('/api/apps', formData);
const res = await axios.post<ApiResponse<App>>('/api/apps', formData, {
headers: applyAuth(),
});

dispatch<any>({
type: ActionType.createNotification,
Expand All @@ -88,7 +99,9 @@ export const addApp =
export const deleteApp =
(id: number) => async (dispatch: Dispatch<DeleteAppAction>) => {
try {
await axios.delete<ApiResponse<{}>>(`/api/apps/${id}`);
await axios.delete<ApiResponse<{}>>(`/api/apps/${id}`, {
headers: applyAuth(),
});

dispatch<any>({
type: ActionType.createNotification,
Expand All @@ -113,7 +126,10 @@ export const updateApp =
try {
const res = await axios.put<ApiResponse<App>>(
`/api/apps/${id}`,
formData
formData,
{
headers: applyAuth(),
}
);

dispatch<any>({
Expand Down Expand Up @@ -155,7 +171,9 @@ export const reorderApps =
})
);

await axios.put<ApiResponse<{}>>('/api/apps/0/reorder', updateQuery);
await axios.put<ApiResponse<{}>>('/api/apps/0/reorder', updateQuery, {
headers: applyAuth(),
});

dispatch({
type: ActionType.reorderApps,
Expand Down
13 changes: 13 additions & 0 deletions client/src/store/action-creators/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LogoutAction,
} from '../actions/auth';
import axios, { AxiosError } from 'axios';
import { getApps, getCategories } from '.';

export const login =
(formData: { password: string; duration: string }) =>
Expand All @@ -24,6 +25,9 @@ export const login =
type: ActionType.login,
payload: res.data.data.token,
});

dispatch<any>(getApps());
dispatch<any>(getCategories());
} catch (err) {
dispatch<any>(authError(err, true));
}
Expand All @@ -35,6 +39,9 @@ export const logout = () => (dispatch: Dispatch<LogoutAction>) => {
dispatch({
type: ActionType.logout,
});

dispatch<any>(getApps());
dispatch<any>(getCategories());
};

export const autoLogin = () => async (dispatch: Dispatch<AutoLoginAction>) => {
Expand All @@ -50,6 +57,9 @@ export const autoLogin = () => async (dispatch: Dispatch<AutoLoginAction>) => {
type: ActionType.autoLogin,
payload: token,
});

dispatch<any>(getApps());
dispatch<any>(getCategories());
} catch (err) {
dispatch<any>(authError(err, false));
}
Expand All @@ -69,4 +79,7 @@ export const authError =
},
});
}

dispatch<any>(getApps());
dispatch<any>(getCategories());
};
31 changes: 22 additions & 9 deletions client/src/store/action-creators/bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NewBookmark,
NewCategory,
} from '../../interfaces';
import { applyAuth } from '../../utility';
import { ActionType } from '../action-types';
import {
AddBookmarkAction,
Expand All @@ -31,7 +32,9 @@ export const getCategories =
});

try {
const res = await axios.get<ApiResponse<Category[]>>('/api/categories');
const res = await axios.get<ApiResponse<Category[]>>('/api/categories', {
headers: applyAuth(),
});

dispatch({
type: ActionType.getCategoriesSuccess,
Expand All @@ -47,7 +50,8 @@ export const addCategory =
try {
const res = await axios.post<ApiResponse<Category>>(
'/api/categories',
formData
formData,
{ headers: applyAuth() }
);

dispatch<any>({
Expand Down Expand Up @@ -75,7 +79,8 @@ export const addBookmark =
try {
const res = await axios.post<ApiResponse<Bookmark>>(
'/api/bookmarks',
formData
formData,
{ headers: applyAuth() }
);

dispatch<any>({
Expand All @@ -101,7 +106,8 @@ export const pinCategory =
const { id, isPinned, name } = category;
const res = await axios.put<ApiResponse<Category>>(
`/api/categories/${id}`,
{ isPinned: !isPinned }
{ isPinned: !isPinned },
{ headers: applyAuth() }
);

const status = isPinned
Expand All @@ -128,7 +134,9 @@ export const pinCategory =
export const deleteCategory =
(id: number) => async (dispatch: Dispatch<DeleteCategoryAction>) => {
try {
await axios.delete<ApiResponse<{}>>(`/api/categories/${id}`);
await axios.delete<ApiResponse<{}>>(`/api/categories/${id}`, {
headers: applyAuth(),
});

dispatch<any>({
type: ActionType.createNotification,
Expand All @@ -153,7 +161,8 @@ export const updateCategory =
try {
const res = await axios.put<ApiResponse<Category>>(
`/api/categories/${id}`,
formData
formData,
{ headers: applyAuth() }
);

dispatch<any>({
Expand All @@ -179,7 +188,9 @@ export const deleteBookmark =
(bookmarkId: number, categoryId: number) =>
async (dispatch: Dispatch<DeleteBookmarkAction>) => {
try {
await axios.delete<ApiResponse<{}>>(`/api/bookmarks/${bookmarkId}`);
await axios.delete<ApiResponse<{}>>(`/api/bookmarks/${bookmarkId}`, {
headers: applyAuth(),
});

dispatch<any>({
type: ActionType.createNotification,
Expand Down Expand Up @@ -218,7 +229,8 @@ export const updateBookmark =
try {
const res = await axios.put<ApiResponse<Bookmark>>(
`/api/bookmarks/${bookmarkId}`,
formData
formData,
{ headers: applyAuth() }
);

dispatch<any>({
Expand Down Expand Up @@ -295,7 +307,8 @@ export const reorderCategories =

await axios.put<ApiResponse<{}>>(
'/api/categories/0/reorder',
updateQuery
updateQuery,
{ headers: applyAuth() }
);

dispatch({
Expand Down
Loading

0 comments on commit d94a6ce

Please sign in to comment.