#53 axios interceptor fix , remove headers in tasksActions

parent e23e8c1b
...@@ -80,6 +80,7 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=> ...@@ -80,6 +80,7 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>
router.put('/',async(req:Request, res:Response)=> { router.put('/',async(req:Request, res:Response)=> {
const token = req.get('Authorization'); const token = req.get('Authorization');
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
...@@ -94,6 +95,7 @@ router.put('/',async(req:Request, res:Response)=> { ...@@ -94,6 +95,7 @@ router.put('/',async(req:Request, res:Response)=> {
.from(Task, "task") .from(Task, "task")
.where("task.id = :id", { id }) .where("task.id = :id", { id })
.getOne() .getOne()
if (!task) return res.status(404).send({Message:'task not found'}) if (!task) return res.status(404).send({Message:'task not found'})
task.title= title task.title= title
task.description= description task.description= description
......
...@@ -35,9 +35,7 @@ return res.send({users}) ...@@ -35,9 +35,7 @@ return res.send({users})
router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => { router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
console.log(req.body)
const {name,surname,password,email, role} = req.body; const {name,surname,password,email, role} = req.body;
console.log(req.body)
const displayName = surname+' '+name[0]+'.' const displayName = surname+' '+name[0]+'.'
const user = new User(); const user = new User();
user.name = name; user.name = name;
...@@ -47,13 +45,10 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response): ...@@ -47,13 +45,10 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
user.email = email; user.email = email;
user.role = role; user.role = role;
user.generateToken() user.generateToken()
console.log("user " + user)
await user.save(); await user.save();
console.log("saved")
const userToFront:User|null = await dataSource.manager.findOneBy(User, { const userToFront:User|null = await dataSource.manager.findOneBy(User, {
email: user.email email: user.email
}) })
console.log("userToFront " + userToFront)
return res.send({userToFront}) return res.send({userToFront})
}) })
......
export const apiUrl = "http://localhost:8001"; export const apiUrl = "http://localhost:8000";
export const uploadsUrl = `${apiUrl}/uploads`; export const uploadsUrl = `${apiUrl}/uploads`;
...@@ -60,7 +60,7 @@ export default function EnhancedTable() { ...@@ -60,7 +60,7 @@ export default function EnhancedTable() {
useEffect(() => { useEffect(() => {
dispatch(fetchAllTasks()); dispatch(fetchAllTasks());
}, []); }, [dispatch]);
const tasks = useSelector((state) => state.tasks.tasks); const tasks = useSelector((state) => state.tasks.tasks);
console.log(tasks) console.log(tasks)
...@@ -178,7 +178,7 @@ console.log(tasks) ...@@ -178,7 +178,7 @@ console.log(tasks)
setRecievedTasks(newTasks); setRecievedTasks(newTasks);
}; };
const HandleEditTask=(task)=>{ const HandleEditTask= (task)=>{
dispatch(editTask(task)) dispatch(editTask(task))
} }
......
...@@ -6,7 +6,7 @@ import { configureStore } from '@reduxjs/toolkit'; ...@@ -6,7 +6,7 @@ import { configureStore } from '@reduxjs/toolkit';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import usersReducer from './store/reducers/usersReducer'; import usersReducer from './store/reducers/usersReducer';
import tasksReducer from './store/reducers/tasksReducer'; import tasksReducer from './store/reducers/tasksReducer';
import axios from 'axios'; import axios from './axiosPlanner';
const localStorageMiddleware = ({getState}) => (next) => (action) => { const localStorageMiddleware = ({getState}) => (next) => (action) => {
const result = next(action); const result = next(action);
...@@ -21,13 +21,6 @@ const loadFromLocalStorage = () => { ...@@ -21,13 +21,6 @@ const loadFromLocalStorage = () => {
return undefined; return undefined;
}; };
axios.interceptors.request.use(config=>{
try{
config.headers['Authorization']=store.getState().users.user.token;
} catch(e){
}
return config;
})
const store = configureStore({ const store = configureStore({
reducer: { reducer: {
...@@ -38,6 +31,14 @@ const store = configureStore({ ...@@ -38,6 +31,14 @@ const store = configureStore({
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware) middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware)
}) })
axios.interceptors.request.use(config=>{
try{
config.headers['Authorization']=store.getState().users.user.token;
} catch(e){
}
return config;
})
const root = ReactDOM.createRoot(document.getElementById('root')); const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( root.render(
<Provider store={store}> <Provider store={store}>
......
...@@ -70,11 +70,7 @@ export const addTask = (task) => { ...@@ -70,11 +70,7 @@ export const addTask = (task) => {
dispatch(addTaskRequest()); dispatch(addTaskRequest());
const token = getState().users?.user?.token; const token = getState().users?.user?.token;
try { try {
await axios.post("/tasks", task, { await axios.post("/tasks", task);
headers: {
'Authorization': 'IwGVRaksGTWtnKlOZd7zJ'
}
});
dispatch(addTaskSuccess()) dispatch(addTaskSuccess())
dispatch(fetchCalendarTasks()) dispatch(fetchCalendarTasks())
} catch (error) { } catch (error) {
...@@ -98,14 +94,10 @@ const editTaskFailure = (error) => { ...@@ -98,14 +94,10 @@ const editTaskFailure = (error) => {
export const editTask = (task) => { export const editTask = (task) => {
return async (dispatch, getState) => { return async (dispatch, getState) => {
dispatch(editTaskRequest()); dispatch(editTaskRequest());
const token = getState().users?.user?.token; // const token = getState().users?.user?.token;
try { try {
console.log(task) console.log('task' , task)
const r=await axios.put("/tasks", task, { const r=await axios.put("/tasks/", task);
headers: {
'Authorization': 'IwGVRaksGTWtnKlOZd7zJ'
}
});
console.log(r) console.log(r)
dispatch(editTaskSuccess()) dispatch(editTaskSuccess())
dispatch(fetchAllTasks()) dispatch(fetchAllTasks())
...@@ -131,13 +123,9 @@ const deleteTaskFailure = (error) => { ...@@ -131,13 +123,9 @@ const deleteTaskFailure = (error) => {
export const deleteTask = (taskId) => { export const deleteTask = (taskId) => {
return async (dispatch, getState) => { return async (dispatch, getState) => {
dispatch(deleteTaskRequest()); dispatch(deleteTaskRequest());
const token = getState().users?.user?.token; // const token = getState().users?.user?.token;
try { try {
await axios.delete(`/tasks/${taskId}`, { await axios.delete(`/tasks/${taskId}`);
headers: {
'Authorization': 'IwGVRaksGTWtnKlOZd7zJ'
}
});
dispatch(deleteTaskSuccess()) dispatch(deleteTaskSuccess())
dispatch(fetchCalendarTasks()) dispatch(fetchCalendarTasks())
dispatch(fetchAllTasks()) dispatch(fetchAllTasks())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment