Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
52 lines
1.2 KiB
Plaintext
52 lines
1.2 KiB
Plaintext
'use server';
|
|
|
|
import { cookies as getCookies, headers as nextHeaders } from 'next/headers.js';
|
|
import { createLocalReq, getPayload, logoutOperation } from 'payload';
|
|
import { getExistingAuthToken } from '../utilities/getExistingAuthToken.js';
|
|
export async function logout({
|
|
allSessions = false,
|
|
config
|
|
}) {
|
|
const payload = await getPayload({
|
|
config,
|
|
cron: true
|
|
});
|
|
const headers = await nextHeaders();
|
|
const authResult = await payload.auth({
|
|
headers
|
|
});
|
|
if (!authResult.user) {
|
|
return {
|
|
message: 'User already logged out',
|
|
success: true
|
|
};
|
|
}
|
|
const {
|
|
user
|
|
} = authResult;
|
|
const req = await createLocalReq({
|
|
user
|
|
}, payload);
|
|
const collection = payload.collections[user.collection];
|
|
const logoutResult = await logoutOperation({
|
|
allSessions,
|
|
collection,
|
|
req
|
|
});
|
|
if (!logoutResult) {
|
|
return {
|
|
message: 'Logout failed',
|
|
success: false
|
|
};
|
|
}
|
|
const existingCookie = await getExistingAuthToken(payload.config.cookiePrefix);
|
|
if (existingCookie) {
|
|
const cookies = await getCookies();
|
|
cookies.delete(existingCookie.name);
|
|
}
|
|
return {
|
|
message: 'User logged out successfully',
|
|
success: true
|
|
};
|
|
}
|
|
//# sourceMappingURL=logout.js.map |