1
1
import NextAuth from "next-auth" ;
2
2
import { NextResponse } from "next/server" ;
3
- import type { Endpoint } from "payload" ;
3
+ import { APIError , type Endpoint , generateExpiredPayloadCookie , headersWithCors } from "payload" ;
4
4
import { withPayload } from "../../../authjs/withPayload" ;
5
+ import { AUTHJS_STRATEGY_NAME } from "../../AuthjsAuthStrategy" ;
5
6
import type { AuthjsPluginConfig } from "../../plugin" ;
6
7
import { getRequestCollection } from "../../utils/getRequestCollection" ;
7
8
@@ -10,30 +11,68 @@ import { getRequestCollection } from "../../utils/getRequestCollection";
10
11
*
11
12
* @see https://payloadcms.com/docs/authentication/operations#logout
12
13
* @see https://github.com/payloadcms/payload/blob/main/packages/payload/src/auth/endpoints/logout.ts
14
+ * @see https://github.com/payloadcms/payload/blob/main/packages/payload/src/auth/operations/logout.ts
13
15
*/
14
16
export const logoutEndpoint : ( pluginOptions : AuthjsPluginConfig ) => Endpoint = pluginOptions => ( {
15
17
method : "post" ,
16
18
path : "/logout" ,
17
19
handler : async req => {
18
- // Sign out and get cookies from authjs
19
- const { signOut } = NextAuth (
20
- withPayload ( pluginOptions . authjsConfig , {
21
- payload : req . payload ,
22
- userCollectionSlug : pluginOptions . userCollectionSlug ,
23
- } ) ,
20
+ const { config : collection } = getRequestCollection ( req ) ;
21
+
22
+ if ( ! req . user ) {
23
+ throw new APIError ( "No User" , 400 ) ;
24
+ }
25
+
26
+ if ( req . user . collection !== collection . slug ) {
27
+ throw new APIError ( "Incorrect collection" , 403 ) ;
28
+ }
29
+
30
+ // Create response with cors headers
31
+ const response = NextResponse . json (
32
+ {
33
+ message : req . t ( "authentication:logoutSuccessful" ) ,
34
+ } ,
35
+ {
36
+ headers : headersWithCors ( {
37
+ headers : new Headers ( ) ,
38
+ req,
39
+ } ) ,
40
+ } ,
24
41
) ;
25
- const { cookies } = await signOut ( { redirect : false } ) ;
26
-
27
- // Create response with cookies
28
- const response = NextResponse . json ( {
29
- message : req . t ( "authentication:logoutSuccessful" ) ,
30
- } ) ;
31
- for ( const cookie of cookies ) {
32
- response . cookies . set ( cookie . name , cookie . value , cookie . options ) ;
42
+
43
+ if ( req . user . _strategy === AUTHJS_STRATEGY_NAME ) {
44
+ // Generate expired cookies using authjs
45
+ const { signOut } = NextAuth (
46
+ withPayload ( pluginOptions . authjsConfig , {
47
+ payload : req . payload ,
48
+ userCollectionSlug : pluginOptions . userCollectionSlug ,
49
+ } ) ,
50
+ ) ;
51
+ const { cookies } = ( await signOut ( { redirect : false } ) ) as {
52
+ cookies : {
53
+ name : string ;
54
+ value : string ;
55
+ options : object ;
56
+ } [ ] ;
57
+ } ;
58
+
59
+ // Set cookies on response
60
+ for ( const cookie of cookies ) {
61
+ response . cookies . set ( cookie . name , cookie . value , cookie . options ) ;
62
+ }
63
+ } else {
64
+ // Generate an expired cookie using payload cms
65
+ const expiredCookie = generateExpiredPayloadCookie ( {
66
+ collectionAuthConfig : collection . auth ,
67
+ config : req . payload . config ,
68
+ cookiePrefix : req . payload . config . cookiePrefix ,
69
+ } ) ;
70
+
71
+ // Set cookie on response
72
+ response . headers . set ( "Set-Cookie" , expiredCookie ) ;
33
73
}
34
74
35
75
// Execute afterLogout hooks
36
- const { config : collection } = getRequestCollection ( req ) ;
37
76
if ( collection . hooks ?. afterLogout ?. length ) {
38
77
for ( const hook of collection . hooks . afterLogout ) {
39
78
await hook ( {
0 commit comments