I am developing this website using python3 backend and react frontend. I tried to set cookies for auth however no matter what setting i try the frontend just does not recognise it, and on refresh the cookie disappears. All the cookie headers work as intended.
Heres my code:
def auth_login(email, password):
match = db.users.find_one({’email’: email, ‘password’: hash(password)})
if match is None:
raise InputError(‘Incorrect email or password’)
session_id, session_end_time = add_login_session(match['_id'])
token = encode_jwt({
'user_id': str(match["_id"]),
'session_id': str(session_id),
'session_end_time': str(session_end_time)
})
# Create a response object
response = make_response({'message': 'Login successful', 'session_end_time': session_end_time})
# Set a cookie on the response object
response.set_cookie(
key='token',
value=token,
httponly=False,
secure=False,
samesite='Lax',
expires=session_end_time,
path='/'
)
return response
CORS setting: CORS(app, expose_headers=’Authorization’, supports_credentials=True, resources={r”/*”: {“origins”: “http://localhost:5173”}})
Frontend check:
useEffect(() => {
const token = Cookies.get('token');
console.log(token)
setAuth(!!token);
if (token) {
setAuth(true);
fetchProfileData(setProfileData);
} else {
setAuth(false);
}
}, []);
I even set up a backend endpoint to getCookie, however when that backend is hit also returned nothing. This means that the cookie was never set, or immediately disappeared, However the expiry datetime is 2 hours in the future. very confused.
Ethan Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.