redux_jwt_auth icon indicating copy to clipboard operation
redux_jwt_auth copied to clipboard

CORS issue

Open rmkanda opened this issue 2 years ago • 4 comments

@gitdagray Thanks for the tutorial and code. It really helps.

I faced CORS issue if we use "credentials: 'include'" when the server has allow orgin as "*". Since we are using API Tokens, do we need this flag? Can we change it to "credentials: 'omit'" ? (Since this flag is only need for cookies)

https://github.com/gitdagray/redux_jwt_auth/blob/44f187232124b9a23398169bdfe97068299a362b/src/app/api/apiSlice.js#L6

rmkanda avatar Jan 02 '23 08:01 rmkanda

@gitdagray Thanks for the tutorial and code. It really helps.

I faced CORS issue if we use "credentials: 'include'" when the server has allow orgin as "*". Since we are using API Tokens, do we need this flag? Can we change it to "credentials: 'omit'" ? (Since this flag is only need for cookies)

https://github.com/gitdagray/redux_jwt_auth/blob/44f187232124b9a23398169bdfe97068299a362b/src/app/api/apiSlice.js#L6

Kindly change include to true

olawale-o avatar Jan 12 '23 16:01 olawale-o

@gitdagray Hi, I followed your tutorials on youtube (very helpful btw) and I'm hitting the CORS issue. I've tried the following:

  1. Changing credentials: "include" to credentials: "same-origin"
  2. Adding "proxy": " in package.json file
  3. Adding http-proxy-middleware as suggested in https://jafreitas90.medium.com/how-to-solve-the-cors-issue-in-a-create-react-app-project-with-a-proxy-5403141d7f32

In the 3rd method, I am getting the following error:

{
    "status": "PARSING_ERROR",
    "originalStatus": 404,
    "data": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /api/path</pre>\n</body>\n</html>\n",
    "error": "SyntaxError: Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON"
}

But nothing seems to work. Is there something I'm missing? Would really appreciate it if I could get some help.

NOTE: I have a grpc server.

morelattes avatar Oct 15 '23 10:10 morelattes

@gitdagray Thanks for the tutorial and code. It really helps.

I faced CORS issue if we use "credentials: 'include'" when the server has allow orgin as "*". Since we are using API Tokens, do we need this flag? Can we change it to "credentials: 'omit'" ? (Since this flag is only need for cookies)

https://github.com/gitdagray/redux_jwt_auth/blob/44f187232124b9a23398169bdfe97068299a362b/src/app/api/apiSlice.js#L6

Any luck in finding the solution?

morelattes avatar Oct 15 '23 10:10 morelattes

const baseQuery = fetchBaseQuery({
	baseUrl: "http://localhost:8000/api",
	prepareHeaders: (headers, { getState }) => {
		headers.set("Access-Control-Allow-Headers", "*");
		const token = getState().auth.token;
		
		if (token) {
			headers.set("Authorization", `Bearer ${token}`);
		}
		return headers;
	},
});

In my case i used

headers.set("Access-Control-Allow-Headers", "*");

I think it will be solve the issue :)

sumit-kr-das avatar Oct 27 '23 20:10 sumit-kr-das