how to logout a user from server side. #3391
Replies: 3 comments 2 replies
-
Has this been reesolved? I found this dicussion through Google Search. |
Beta Was this translation helpful? Give feedback.
-
My need was to sign out a user from a third party app. So here's the function that is part of the auth public API script : const SIGN_OUT_API_URI_SUFFIX = '/auth/api/signout';
const CSRF_API_URI_SUFFIX = '/auth/api/csrf';
/**
* @returns {Promise<any>}
*/
function signOut() {
return getFingerprint().then(fingerprint => {
return fetch(getServerUrl(CSRF_API_URI_SUFFIX)).then(response => response.text()).then(csrfBody => {
return fetch(getServerUrl(SIGN_OUT_API_URI_SUFFIX, getServerUrl(STATE_URI_SUFFIX)), {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-API-Fingerprint': fingerprint
},
body: csrfBody
}).then(response => response.json());
})
});
}; We use Second, we fetch the CSRF token because it is required in order to validate the sign out query. I have NO idea why this is required since the sign out query has access to the same cookie... this is redundant to me, but this is the requirement! Third, the request must specify a JSON data type on both the query params and the result. This is important otherwise the sign out API will redirect to the sign in HTML page, and we do not need that, just the success state. My personal opinion is that Next Auth fails to provide a clean and unopinionated API. Every feature should be available as callable JS functions without relying on routes, whereas API routes should be JSON by default and not automatically redirect to HTML. Default pages should make use of both JS functions and API routes internally, so that custom pages could do just the same but with user components. If Next Auth should have a major release, this should be the focus; unopinionated auth API (bring your own components, e.g. |
Beta Was this translation helpful? Give feedback.
-
Maybe this would be helpful #5334 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
I can't find a way to remove the next-auth.session-token from cookies
How to reproduce ☕️
I tried the follwing, but I don't know if removing cookies on client side is safe, what should I do?
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions