Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/sdk/src/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { PayloadSDK } from '../index.js'
import type {
AuthCollectionSlug,
PayloadGeneratedTypes,
} from '../types.js'

export type LogoutOptions<
T extends PayloadGeneratedTypes,
TSlug extends AuthCollectionSlug<T>
> = {
collection: TSlug
allSessions?: boolean
}

export type LogoutResult = {
message: string
}

export async function logout<
T extends PayloadGeneratedTypes,
TSlug extends AuthCollectionSlug<T>
>(
sdk: PayloadSDK<T>,
options: LogoutOptions<T, TSlug>,
init?: RequestInit,
): Promise<LogoutResult> {
const response = await sdk.request({
init,
method: 'POST',
path: `/${options.collection}/logout?allSessions=${options.allSessions ?? false}`,
})

return response.json()
}
9 changes: 9 additions & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { findGlobalVersions } from './globals/findVersions.js'
import { restoreGlobalVersion } from './globals/restoreVersion.js'
import { updateGlobal } from './globals/update.js'
import { buildSearchParams } from './utilities/buildSearchParams.js'
import { logout, LogoutOptions, LogoutResult } from './auth/logout.js'

type Args = {
/** Base passed `RequestInit` to `fetch`. For base headers / credentials include etc. */
Expand Down Expand Up @@ -371,4 +372,12 @@ export class PayloadSDK<T extends PayloadGeneratedTypes = PayloadGeneratedTypes>
): Promise<{ message: string }> {
return verifyEmail(this, options, init)
}

logout<TSlug extends AuthCollectionSlug<T>>(
options: LogoutOptions<T, TSlug>,
init?: RequestInit,
): Promise<LogoutResult> {
return logout(this, options, init)
}

}
24 changes: 24 additions & 0 deletions test/sdk/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,28 @@

expect(email).toBe(user.email)
})

it('should execute logout', async () => {
const loginRes = await sdk.login({
collection: 'users',
data: { email: testUserCredentials.email, password: testUserCredentials.password },
})

expect(loginRes.user.email).toBe(testUserCredentials.email)

const logoutRes = await sdk.logout({
collection: 'users',
allSessions: false,
})

expect(logoutRes.message).toBeDefined()

await expect(

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-mongodb

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 9615500, Symbol(trigger_async_id_symbol): 9615336}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-firestore

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 9943167, Symbol(trigger_async_id_symbol): 9942999}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite-uuid

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 4486957, Symbol(trigger_async_id_symbol): 4486891}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres-custom-schema

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 5243124, Symbol(trigger_async_id_symbol): 5243057}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 5242821, Symbol(trigger_async_id_symbol): 5242754}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 4491291, Symbol(trigger_async_id_symbol): 4491225}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres-uuid

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 5249647, Symbol(trigger_async_id_symbol): 5249580}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)

Check failure on line 325 in test/sdk/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-supabase

@payloadcms/sdk › should execute logout

expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {Symbol(state): {"aborted": false, "body": {"length": null, "source": null, "stream": {Symbol(kType): "ReadableStream", Symbol(kState): {"controller": {Symbol(kType): "ReadableByteStreamController", Symbol(kState): {"autoAllocateChunkSize": undefined, "byobRequest": null, "cancelAlgorithm": [Function nonOpCancel], "closeRequested": false, "highWaterMark": 0, "pendingPullIntos": [], "pullAgain": false, "pullAlgorithm": [Function anonymous], "pulling": false, "queue": [], "queueTotalSize": 0, "started": true, "stream": [Circular]}}, "disturbed": false, "reader": undefined, "state": "readable", "storedError": undefined, "transfer": {"port1": undefined, "port2": undefined, "promise": undefined, "writable": undefined}}, Symbol(nodejs.webstream.isClosedPromise): {"promise": {Symbol(async_id_symbol): 5237985, Symbol(trigger_async_id_symbol): 5237918}, "reject": [Function anonymous], "resolve": [Function anonymous]}, Symbol(nodejs.webstream.controllerErrorFunction): [Function anonymous]}}, "cacheState": "", "headersList": {"cookies": null, Symbol(headers map): Map {"access-control-allow-methods" => {"name": "Access-Control-Allow-Methods", "value": "PUT, PATCH, POST, GET, DELETE, OPTIONS"}, "access-control-allow-headers" => {"name": "Access-Control-Allow-Headers", "value": "Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding, x-apollo-tracing, X-Payload-HTTP-Method-Override"}, "content-type" => {"name": "content-type", "value": "application/json"}}, Symbol(headers map sorted): null}, "rangeRequested": false, "requestIncludesCredentials": false, "status": 200, "statusText": "", "timingAllowPassed": false, "timingInfo": null, "type": "default", "urlList": []}, Symbol(headers): {}} at expect (../node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:113:15) at Object.expect (sdk/int.spec.ts:325:11)
sdk.request({
method: 'GET',
path: '/users/me',
}),
).rejects.toThrow()
})

})
Loading