Skip to content

Commit b2498b4

Browse files
committed
fix: Override payload refresh operation & fix session expiration (#12)
- Add refresh hook - Fix session expiration date inside me hook - Set payload auth strategy name (_strategy)
1 parent 957f589 commit b2498b4

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

packages/dev/src/auth.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ export const authConfig: NextAuthConfig = {
8383
],
8484
session: {
8585
strategy: "jwt",
86+
//maxAge: 60 * 2 + 30, // 2.5 minutes
87+
//updateAge: 60, // 1 minute
8688
},
8789
callbacks: {
8890
jwt: ({ token, user, account, trigger }) => {
89-
//console.log("callbacks.jwt", token, user, account);
91+
//console.log("callbacks.jwt", trigger, token, user, account);
9092

9193
/**
9294
* For jwt session strategy, we need to forward additional fields to the token

packages/payload-authjs/src/payload/AuthjsAuthStrategy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function AuthjsAuthStrategy(
1717
const virtualFields = getAllVirtualFields(collection.fields);
1818

1919
return {
20-
name: "authjs",
20+
name: "Auth.js",
2121
authenticate: async ({ payload }) => {
2222
// Get session from authjs
2323
const { auth } = NextAuth(
@@ -62,6 +62,7 @@ export function AuthjsAuthStrategy(
6262
// Return user to payload cms
6363
return {
6464
user: {
65+
_strategy: "Auth.js",
6566
collection: collection.slug,
6667
...payloadUser,
6768
...virtualSessionFields,

packages/payload-authjs/src/payload/collection/hooks/me.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const meHook: (
4444

4545
// Return user to payload cms
4646
return {
47-
exp: new Date(session.expires).getTime(),
47+
exp: Math.floor(new Date(session.expires).getTime() / 1000),
4848
user: {
4949
...user,
5050
...virtualSessionFields,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import NextAuth from "next-auth";
2+
import { CollectionConfig, Forbidden, type CollectionRefreshHook } from "payload";
3+
import { withPayload } from "../../../authjs/withPayload";
4+
import type { AuthjsPluginConfig } from "../../plugin";
5+
import { getAllVirtualFields } from "../../utils/getAllVirtualFields";
6+
import { getUserAttributes } from "../../utils/getUserAttributes";
7+
8+
/**
9+
* Add refresh hook to override the refresh endpoint to refresh the session with authjs
10+
*
11+
* @see https://payloadcms.com/docs/hooks/collections#refresh
12+
* @see https://github.com/payloadcms/payload/blob/main/packages/payload/src/auth/operations/refresh.ts
13+
*/
14+
export const refreshHook: (
15+
collection: CollectionConfig,
16+
pluginOptions: AuthjsPluginConfig,
17+
) => CollectionRefreshHook | undefined = (collection, pluginOptions) => {
18+
// Get all virtual fields
19+
const virtualFields = getAllVirtualFields(collection.fields);
20+
21+
// Return the refresh hook
22+
return async ({ args: { req }, user }) => {
23+
// Get session from authjs
24+
const { auth } = NextAuth(
25+
withPayload(pluginOptions.authjsConfig, {
26+
payload: req.payload,
27+
userCollectionSlug: pluginOptions.userCollectionSlug,
28+
}),
29+
);
30+
let session = await auth();
31+
32+
// If no session user, throw forbidden
33+
if (!session?.user) {
34+
throw new Forbidden(req.t);
35+
}
36+
37+
// Get user virtual fields
38+
const virtualSessionFields = getUserAttributes(session.user, virtualFields);
39+
40+
// Return user to payload cms
41+
return {
42+
exp: Math.floor(new Date(session.expires).getTime() / 1000),
43+
setCookie: undefined,
44+
refreshedToken: undefined as unknown as string,
45+
strategy: user._strategy,
46+
user: {
47+
...user,
48+
...virtualSessionFields,
49+
},
50+
};
51+
};
52+
};

packages/payload-authjs/src/payload/collection/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { generalFields } from "./fields/general";
1010
import { sessionsField } from "./fields/session";
1111
import { verificationTokensField } from "./fields/verificationTokens";
1212
import { meHook } from "./hooks/me";
13+
import { refreshHook } from "./hooks/refresh";
1314

1415
export const generateUsersCollection = (
1516
collections: CollectionConfig[],
@@ -93,9 +94,11 @@ export const generateUsersCollection = (
9394

9495
// Add hooks to users collection
9596
const _meHook = meHook(collection, pluginOptions);
97+
const _refreshHook = refreshHook(collection, pluginOptions);
9698
collection.hooks = {
9799
...collection.hooks,
98100
me: [...(collection.hooks?.me || []), ...(_meHook ? [_meHook] : [])],
101+
refresh: [...(collection.hooks?.refresh || []), ...(_refreshHook ? [_refreshHook] : [])],
99102
};
100103

101104
// Add custom endpoints to users collection

0 commit comments

Comments
 (0)