Hi Support team, I deployed my react app on netlify which has backend deployed in heroku. My backend is cookie based authentication system. When user login successsfully, they get cookie in browser. It works fine on local host but after deploying in netlify, my browser doesnot receive cookies. I think it is cors problem. I am using __redirect file to redirect proxies.
My backend controller when someone login successfully is like this
const loginAdmin = asyncHandler(async(req,res)=>{
const {email,password,code} = req.body
if(!email || !password || !code){
throw new Error('Every field is required')
}
const user = await User.findOne({email})
if(!user || (!await user.comparePassword(password,user.password)) ){
throw new Error('Invalid password or email')
}
if(user.role!=='admin'){
throw new Error("You arenot admin")
}
if(code !== process.env.ADMIN_SECRET_key){
throw new Error('Invalid code')
}
const token = await user.generateToken(3600*5)
res.cookie('token',token,{
httpOnly:true,
maxAge:3600000*5,
secure:true,
sameSite:'none',
domain: '.netlify.app'
})
res.json({message:'Welcome admin '})
})
In my frontend I am calling api like this
export const loginAdmin = (body)=> async dispatch => {
dispatch({type:LOGIN_ADMIN_REQ})
try {
const config = {
headers: {
'Content-Type': 'application/json',
},
};
await axios.post(`/api/admin/adminlogin`,body,config)
dispatch({type:LOGIN_ADMIN_SUCCESS})
dispatch(loadUser())
} catch (error) {
dispatch({
type: LOGIN_ADMIN_FAIL,
payload:error.response && error.response.data.message ? error.response.data.message
: error.message
})
}
}
After login , I should make request with backend with cookies for authorization, which I am not getting
export const loadUser = ()=> async dispatch=>{
try {
const config = {
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
mode:'cors',
credentials:'include',
withCredentials:true
}
const res = await axios.get(`/api/admin`,config)
dispatch({
type: LOAD_ADMIN_SUCCESS,
payload: res.data
})
} catch (error) {
console.log(error.response)
dispatch({
type: LOAD_ADMIN_FAIL,
payload:error.response && error.response.data.message ? error.response.data.message
: error.message
})
}
}
api response is ok i.e. login is successful but I am not getting cookie in my browser due to which I cannot get user information. Any way to solve it? My site is https://mobyletech-admin.netlify.app