I am making an admin panel for my telegram bot using React and Nest.js
The logout endpoint should be handled by the backend server which is localhost:3000 (localhost:3001 is my frontend) but calling localhost:3001/auth/logout works and localhost:3000/auth/logout does not
<code>using await fetch(`localhost:3001/auth/logout`, works but await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, doesnt work, it should work with 3000(backend) not 3001(frontend), its the same with all the other endpoints too.
</code>
<code>using await fetch(`localhost:3001/auth/logout`, works but await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, doesnt work, it should work with 3000(backend) not 3001(frontend), its the same with all the other endpoints too.
</code>
using await fetch(`localhost:3001/auth/logout`, works but await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, doesnt work, it should work with 3000(backend) not 3001(frontend), its the same with all the other endpoints too.
<code> REACT_APP_API_URL=http://localhost:3000
</code>
<code> REACT_APP_API_URL=http://localhost:3000
</code>
REACT_APP_API_URL=http://localhost:3000
<code>const handleSignOut = async () => {
await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, {
method: 'GET',
credentials: 'include' // necessary for cookies if sessions are used
})
.then(response => {
if (response.ok) {
localStorage.removeItem('user'); // Clear local storage if used
sessionStorage.removeItem('user'); // Clear session storage if used
window.location.href = `http://localhost:3001/admin`; // Redirect after successful logout
} else {
throw new Error('Logout failed with status: ' + response.status);
}
})
.catch(error => {
console.error('Logout failed:', error);
});
};
</code>
<code>const handleSignOut = async () => {
await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, {
method: 'GET',
credentials: 'include' // necessary for cookies if sessions are used
})
.then(response => {
if (response.ok) {
localStorage.removeItem('user'); // Clear local storage if used
sessionStorage.removeItem('user'); // Clear session storage if used
window.location.href = `http://localhost:3001/admin`; // Redirect after successful logout
} else {
throw new Error('Logout failed with status: ' + response.status);
}
})
.catch(error => {
console.error('Logout failed:', error);
});
};
</code>
const handleSignOut = async () => {
await fetch(`${process.env.REACT_APP_API_URL}/auth/logout`, {
method: 'GET',
credentials: 'include' // necessary for cookies if sessions are used
})
.then(response => {
if (response.ok) {
localStorage.removeItem('user'); // Clear local storage if used
sessionStorage.removeItem('user'); // Clear session storage if used
window.location.href = `http://localhost:3001/admin`; // Redirect after successful logout
} else {
throw new Error('Logout failed with status: ' + response.status);
}
})
.catch(error => {
console.error('Logout failed:', error);
});
};
<code> @Get('logout')
async logout(@Req() req, @Res() res, @Session() session) {
req.session.destroy((err) => {
if (err) {
console.error('Session destruction failed:', err);
res.status(500).json({ message: 'Failed to log out' });
} else {
res.redirect(`${process.env.REACT_APP_DASHBOARD_URL}/admin`);
}
});
}
</code>
<code> @Get('logout')
async logout(@Req() req, @Res() res, @Session() session) {
req.session.destroy((err) => {
if (err) {
console.error('Session destruction failed:', err);
res.status(500).json({ message: 'Failed to log out' });
} else {
res.redirect(`${process.env.REACT_APP_DASHBOARD_URL}/admin`);
}
});
}
</code>
@Get('logout')
async logout(@Req() req, @Res() res, @Session() session) {
req.session.destroy((err) => {
if (err) {
console.error('Session destruction failed:', err);
res.status(500).json({ message: 'Failed to log out' });
} else {
res.redirect(`${process.env.REACT_APP_DASHBOARD_URL}/admin`);
}
});
}
CORs is enabled still isnt working
New contributor
Abhinna Shyam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.