mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-07 03:35:18 +02:00
Added auth headers to api requests
This commit is contained in:
parent
0d36c5cf94
commit
d94a6cea5a
12 changed files with 135 additions and 56 deletions
|
@ -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[]>>) => {
|
||||
|
@ -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,
|
||||
|
@ -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'
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -113,7 +126,10 @@ export const updateApp =
|
|||
try {
|
||||
const res = await axios.put<ApiResponse<App>>(
|
||||
`/api/apps/${id}`,
|
||||
formData
|
||||
formData,
|
||||
{
|
||||
headers: applyAuth(),
|
||||
}
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
|
@ -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,
|
||||
|
|
|
@ -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 }) =>
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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>) => {
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -69,4 +79,7 @@ export const authError =
|
|||
},
|
||||
});
|
||||
}
|
||||
|
||||
dispatch<any>(getApps());
|
||||
dispatch<any>(getCategories());
|
||||
};
|
||||
|
|
|
@ -8,6 +8,7 @@ import {
|
|||
NewBookmark,
|
||||
NewCategory,
|
||||
} from '../../interfaces';
|
||||
import { applyAuth } from '../../utility';
|
||||
import { ActionType } from '../action-types';
|
||||
import {
|
||||
AddBookmarkAction,
|
||||
|
@ -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,
|
||||
|
@ -47,7 +50,8 @@ export const addCategory =
|
|||
try {
|
||||
const res = await axios.post<ApiResponse<Category>>(
|
||||
'/api/categories',
|
||||
formData
|
||||
formData,
|
||||
{ headers: applyAuth() }
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
|
@ -75,7 +79,8 @@ export const addBookmark =
|
|||
try {
|
||||
const res = await axios.post<ApiResponse<Bookmark>>(
|
||||
'/api/bookmarks',
|
||||
formData
|
||||
formData,
|
||||
{ headers: applyAuth() }
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -153,7 +161,8 @@ export const updateCategory =
|
|||
try {
|
||||
const res = await axios.put<ApiResponse<Category>>(
|
||||
`/api/categories/${id}`,
|
||||
formData
|
||||
formData,
|
||||
{ headers: applyAuth() }
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
|
@ -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,
|
||||
|
@ -218,7 +229,8 @@ export const updateBookmark =
|
|||
try {
|
||||
const res = await axios.put<ApiResponse<Bookmark>>(
|
||||
`/api/bookmarks/${bookmarkId}`,
|
||||
formData
|
||||
formData,
|
||||
{ headers: applyAuth() }
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
|
@ -295,7 +307,8 @@ export const reorderCategories =
|
|||
|
||||
await axios.put<ApiResponse<{}>>(
|
||||
'/api/categories/0/reorder',
|
||||
updateQuery
|
||||
updateQuery,
|
||||
{ headers: applyAuth() }
|
||||
);
|
||||
|
||||
dispatch({
|
||||
|
|
|
@ -18,7 +18,7 @@ import {
|
|||
WeatherForm,
|
||||
} from '../../interfaces';
|
||||
import { ActionType } from '../action-types';
|
||||
import { storeUIConfig } from '../../utility';
|
||||
import { storeUIConfig, applyAuth } from '../../utility';
|
||||
|
||||
const keys: (keyof Config)[] = [
|
||||
'useAmericanDate',
|
||||
|
@ -55,7 +55,13 @@ export const updateConfig =
|
|||
) =>
|
||||
async (dispatch: Dispatch<UpdateConfigAction>) => {
|
||||
try {
|
||||
const res = await axios.put<ApiResponse<Config>>('/api/config', formData);
|
||||
const res = await axios.put<ApiResponse<Config>>(
|
||||
'/api/config',
|
||||
formData,
|
||||
{
|
||||
headers: applyAuth(),
|
||||
}
|
||||
);
|
||||
|
||||
dispatch<any>({
|
||||
type: ActionType.createNotification,
|
||||
|
@ -96,7 +102,9 @@ export const fetchQueries =
|
|||
export const addQuery =
|
||||
(query: Query) => async (dispatch: Dispatch<AddQueryAction>) => {
|
||||
try {
|
||||
const res = await axios.post<ApiResponse<Query>>('/api/queries', query);
|
||||
const res = await axios.post<ApiResponse<Query>>('/api/queries', query, {
|
||||
headers: applyAuth(),
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: ActionType.addQuery,
|
||||
|
@ -111,7 +119,10 @@ export const deleteQuery =
|
|||
(prefix: string) => async (dispatch: Dispatch<DeleteQueryAction>) => {
|
||||
try {
|
||||
const res = await axios.delete<ApiResponse<Query[]>>(
|
||||
`/api/queries/${prefix}`
|
||||
`/api/queries/${prefix}`,
|
||||
{
|
||||
headers: applyAuth(),
|
||||
}
|
||||
);
|
||||
|
||||
dispatch({
|
||||
|
@ -129,7 +140,10 @@ export const updateQuery =
|
|||
try {
|
||||
const res = await axios.put<ApiResponse<Query[]>>(
|
||||
`/api/queries/${oldPrefix}`,
|
||||
query
|
||||
query,
|
||||
{
|
||||
headers: applyAuth(),
|
||||
}
|
||||
);
|
||||
|
||||
dispatch({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue