axios 헤더에 토큰 넣기

BirdsOnTree·2022년 8월 17일
0

Javascript

목록 보기
8/17
post-thumbnail

쿠키에 있는 토큰을 axios 헤더에 넣어서 보내서 허락을 받아야 할때,
' Bearer '는 서버에서도 맞춰줘야 한다.
thunk, axios.post

export const '이름' = createAsyncThunk("post", async (data) => {
	const response = await axios.post(`api`, data, 
         {
          headers: {
            Authorization: `Bearer ${토큰}`,
         },
});
const headers = {
  Authorization:`Bearer ${토큰}`
}

export const '이름' = createAsyncThunk('post', async (data) => {
	const response = await axios.post('api', data, {headers}
}

header에 들어갈 양이 많아지면 이렇게 써야 가독성이 좋아질것 같다.

export const '이름' = createAsyncThunk('post' async (data) => {
	const response = await axios.post('api', data, { withCredentials: true }
}

withCredentials : true로도 쿠키에서 토큰값을 보내줄수 있다.
물론 서버에서도 Credential 설정을 true를 해주어야 한다.

0개의 댓글