"use client"; import { Input } from "@/components/ui/input"; import { Label } from…
- User
"use client";
import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { LogIn, LogOut, SendHorizontal } from "lucide-react"; import { useState } from "react"; import axios from "axios";
export default function Home() { const [email, setEmail] = useState(""); const [password, setPassword] = useState("");
const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); try { let result = await axios.post( "http://localhost:4000/api/users/login", { email, password, }, { withCredentials: true } ); console.log(result); } catch (error) { console.log(error); } };
const handleLogout = async () => { let result = await axios.post("http://localhost:4000/api/users/logout"); console.log(result.data); };
return ( Sign In Email <Input onChange={(e) => setEmail(e.target.value)} value={email} type="email" id="email" placeholder="Email" /> Password <Input onChange={(e) => setPassword(e.target.value)} value={password} type="password" id="password" placeholder="Password" /> Logout Login ); }
show me this error "Access to XMLHttpRequest at 'http://localhost:4000/api/users/login' from origin 'http://localhost: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."
my backend is payload cms and i already cors set to "*"
- Assistant
The error message you're encountering indicates a CORS (Cross-Origin Resource Sharing) issue where the server's configuration is not allowing credentials (such as cookies or HTTP authentication) to be sent along with the request from your frontend application.
When
withCredentials: trueis used in an Axios request, the CORS policy requires that the server'sAccess-Control-Allow-Originheader specify the exact origin of the request rather than a wildcard (*). Additionally, the server must include theAccess-Control-Allow-Credentials: trueheader to allow credentials to be sent.Here's what you need to do to resolve this issue:
-
Specify the Exact Origin:
-