먼저 URL 뒤에 붙은 쿼리스트링을 가져와야한다.
useLocation
메서드를 통해 location.search
로 쿼리스트링을 불러온다.
const location = useLocation();
console.log(location.search)
URLSearchParams
메서드는 복잡하게 붙은 파라미터들을 쪼개주는 역할이다.
주로 new
를 사용해서 생성자를 통해 초기화하고 사용한다.
url 형태는 http://localhost:3000/token=1&foo=2
라가정하면
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const queryParam1 = searchParams.get("token");
const queryParam2 = searchParams.get('foo')
console.log(queryParam1 , queryParam2) // 절차적으로 파라미터 값을 가져온다. 출력 : 1 , 2
이제 받아온 파라미터(토큰)를 axios로 헤더에 저장하면 된다.
bearer
는 서버랑 동일시 해야함
export const 'requestToken' = createAsyncThunk("post", async (data) => {
const response = await axios.post(`api`, data,
{
headers: {
Authorization: `Bearer ${토큰}`,
},
});