I have a laravel application in which a build my api and i want to use react js on my front-end using the api from my laravel.
but when ever I tried calling the api from my react-app i get this error from my console
Access to XMLHttpRequest at 'http://magicsender.alresia.com/api/v1/auth/login' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
i am just stock here because I have browsed all search engine I couldn't find the right answer to my question.
Here is my code
import { useRef, useState, useEffect, useContext } from 'react';
import AuthContext from "../../app/context/AuthProvider";
import axios from '../../app/api/axios';
const LOGIN_URL = '/auth/login';
const Login = () => {
const { setAuth } = useContext(AuthContext);
const userRef = useRef();
const errRef = useRef();
const [user, setUser] = useState('');
const [pwd, setPwd] = useState('');
const [errMsg, setErrMsg] = useState('');
const [success, setSuccess] = useState(false);
useEffect(() => {
userRef.current.focus();
}, [])
useEffect(() => {
setErrMsg('');
}, [user, pwd])
const handleSubmit = async (e) => {
e.preventDefault();
try {
const response = await axios.post(LOGIN_URL,
JSON.stringify({ user, pwd }),
{
headers: { 'Content-Type': 'application/json' },
withCredentials: true
}
);
console.log(JSON.stringify(response?.data));
//console.log(JSON.stringify(response));
const accessToken = response?.data?.accessToken;
const roles = response?.data?.roles;
setAuth({ user, pwd, roles, accessToken });
setUser('');
setPwd('');
setSuccess(true);
} catch (err) {
if (!err?.response) {
setErrMsg('No Server Response');
} else if (err.response?.status === 400) {
setErrMsg('Missing Username or Password');
} else if (err.response?.status === 401) {
setErrMsg('Unauthorized');
} else {
setErrMsg('Login Failed');
}
errRef.current.focus();
}
}
return (
<>
{success ? (
<section>
<h1>You are logged in!</h1>
<br />
<p>
<a href="#">Go to Home</a>
</p>
</section>
) : (
<section>
<p ref={errRef} className={errMsg ? "errmsg" : "offscreen"} aria-live="assertive">{errMsg}</p>
<h1>Sign In</h1>
<form onSubmit={handleSubmit}>
<label htmlFor="username">Username:</label>
<input
type="text"
id="username"
ref={userRef}
autoComplete="off"
onChange={(e) => setUser(e.target.value)}
value={user}
required
/>
<label htmlFor="password">Password:</label>
<input
type="password"
id="password"
onChange={(e) => setPwd(e.target.value)}
value={pwd}
required
/>
<button>Sign In</button>
</form>
<p>
Need an Account?<br />
<span className="line">
{/*put router link here*/}
<a href="#">Sign Up</a>
</span>
</p>
</section>
)}
</>
)
}
export default Login
Here is the code in ../../app/api/axios
import axios from 'axios';
export default axios.create({
baseURL: 'http://magicsender.alresia.com/api/v1'
});
Please any help will be much appreciated I am not too good in NodeJS but I've been using laravel for a long time