I’m currently working on a web application using ASP.NET Core for the backend with Identity and OpenIddict for authentication, and Next.js for the frontend with Next Auth v5. I’m facing issues with implementing the sign-out functionality correctly.
On the server side, I have the following setup for the logout action:
[Authorize]
[ActionName(nameof(Logout)), HttpPost("~/connect/logout")]
public async Task<IActionResult> LogoutPost()
{
await _signInManager.SignOutAsync();
return SignOut(
authenticationSchemes: OpenIddictServerAspNetCoreDefaults.AuthenticationScheme,
properties: new AuthenticationProperties
{
RedirectUri = "/"
});
}
And on the client side (Next.js with Next Auth v5), I’m attempting to manually trigger the sign-out process by sending a POST request to the logout endpoint:
import { signIn, signOut, auth } from "@/lib/auth"
import { Button } from "./ui/button"
const logOut = async () => {
try {
const session = await auth();
const params = new URLSearchParams();
params.append('token ', session?.accessToken as string);
const response = await fetch("https://localhost:7277/connect/logout", {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params
});
console.log(params);
// Log the response body
if (!response.ok) {
throw new Error('Failed to log out');
}
await signOut();
// Perform client-side sign-out if needed
} catch (error) {
console.error('Error logging out:', error);
throw error;
}
};
However, the sign-out process is not successful. I can’t invoke the LogoutPost action, and there are no error messages returned. I suspect there might be an issue with how I’m handling the access token or the server-side logout process.
What could I be missing here?