Skip to content

Commit 3f1711d

Browse files
committed
chore(dev): Add currentAccount field
1 parent 1999a20 commit 3f1711d

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

packages/dev/src/auth.config.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ declare module "next-auth/jwt" {
1818
extends Partial<
1919
Pick<
2020
PayloadUser,
21-
"id" | "additionalUserDatabaseField" | "additionalUserVirtualField" | "roles"
21+
| "id"
22+
| "additionalUserDatabaseField"
23+
| "additionalUserVirtualField"
24+
| "roles"
25+
| "currentAccount"
2226
>
2327
> {}
2428
}
@@ -121,6 +125,21 @@ export const authConfig: NextAuthConfig = {
121125
token.roles = [...new Set(roles)];
122126
}
123127

128+
/**
129+
* Add current account to the token
130+
*/
131+
if (account) {
132+
token.currentAccount = {
133+
provider: account.provider,
134+
providerAccountId: account.providerAccountId,
135+
access_token: account.access_token,
136+
refresh_token: account.refresh_token,
137+
expires_at: account.expires_at
138+
? new Date(account.expires_at * 1000).toISOString()
139+
: undefined,
140+
};
141+
}
142+
124143
return token;
125144
},
126145
session: ({ session, token }) => {
@@ -138,6 +157,8 @@ export const authConfig: NextAuthConfig = {
138157
session.user.additionalUserVirtualField = token.additionalUserVirtualField;
139158

140159
session.user.roles = token.roles;
160+
161+
session.user.currentAccount = token.currentAccount;
141162
}
142163

143164
return session;

packages/dev/src/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { withPayload } from "payload-authjs";
44
import { authConfig } from "./auth.config";
55

66
export const { handlers, signIn, signOut, auth } = NextAuth(
7-
withPayload(authConfig as any, {
7+
withPayload(authConfig, {
88
payloadConfig,
99
events: {
1010
/**
@@ -18,9 +18,9 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
1818
id: user.id,
1919
name: profile.name ?? (profile.login as string | undefined),
2020
image: profile.avatar_url as string | undefined,
21-
additionalUserDatabaseField: `Create by updateUserOnSignIn at ${new Date().toISOString()}`,
21+
additionalUserDatabaseField: `Create by signIn event at ${new Date().toISOString()}`,
2222
});
2323
},
2424
},
25-
}) as any,
25+
}),
2626
);

packages/dev/src/payload-types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ export interface User {
8282
createdAt: string;
8383
}[]
8484
| null;
85+
currentAccount?: {
86+
provider?: string | null;
87+
providerAccountId?: string | null;
88+
access_token?: string | null;
89+
refresh_token?: string | null;
90+
expires_at?: string | null;
91+
};
8592
verificationTokens?:
8693
| {
8794
id?: string | null;
@@ -185,6 +192,15 @@ export interface UsersSelect<T extends boolean = true> {
185192
additionalAccountDatabaseField?: T;
186193
createdAt?: T;
187194
};
195+
currentAccount?:
196+
| T
197+
| {
198+
provider?: T;
199+
providerAccountId?: T;
200+
access_token?: T;
201+
refresh_token?: T;
202+
expires_at?: T;
203+
};
188204
verificationTokens?:
189205
| T
190206
| {

packages/dev/src/payload/collections/users.ts

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,53 @@ const Users: CollectionConfig = {
3333
custom: {
3434
originalTabLabel: "Accounts",
3535
},
36-
fields: [],
36+
fields: [
37+
/**
38+
* Add currentAccount field
39+
* This field is virtual and will not be stored in the database
40+
*/
41+
{
42+
name: "currentAccount",
43+
type: "group",
44+
label: "Current Account",
45+
virtual: true,
46+
admin: {
47+
hidden: true,
48+
},
49+
fields: [
50+
{
51+
type: "row",
52+
fields: [
53+
{
54+
name: "provider",
55+
type: "text",
56+
},
57+
{
58+
name: "providerAccountId",
59+
type: "text",
60+
},
61+
],
62+
},
63+
{
64+
type: "row",
65+
fields: [
66+
{
67+
name: "access_token",
68+
type: "text",
69+
},
70+
{
71+
name: "refresh_token",
72+
type: "text",
73+
},
74+
{
75+
name: "expires_at",
76+
type: "date",
77+
},
78+
],
79+
},
80+
],
81+
},
82+
],
3783
},
3884
],
3985
},
@@ -46,11 +92,12 @@ const Users: CollectionConfig = {
4692
type: "text",
4793
label: "Account Provider", // Add label to provider field
4894
},
49-
// Add new field to accounts
95+
// Add access_token field
5096
{
5197
name: "access_token",
5298
type: "text",
5399
},
100+
// Add custom field
54101
{
55102
name: "additionalAccountDatabaseField",
56103
type: "text",
@@ -69,12 +116,13 @@ const Users: CollectionConfig = {
69116
type: "array",
70117
fields: [createdAtField],
71118
},
72-
// Add custom field
119+
// Add custom database fields
73120
{
74121
name: "additionalUserDatabaseField",
75122
type: "text",
76123
required: true,
77124
},
125+
// Add custom virtual field
78126
{
79127
name: "additionalUserVirtualField",
80128
type: "text",
@@ -83,18 +131,24 @@ const Users: CollectionConfig = {
83131
hidden: true,
84132
},
85133
},
134+
/**
135+
* Add locale field
136+
*/
86137
{
87138
name: "locale",
88139
type: "text",
89140
},
90141
/**
91142
* Add roles field
92-
* This field will not be stored in the database
143+
* This field is virtual and will not be stored in the database
93144
*/
94145
{
95146
name: "roles",
96147
type: "json",
97148
virtual: true,
149+
admin: {
150+
hidden: true,
151+
},
98152
typescriptSchema: [
99153
() => ({
100154
type: "array",
@@ -103,9 +157,6 @@ const Users: CollectionConfig = {
103157
},
104158
}),
105159
],
106-
admin: {
107-
hidden: true,
108-
},
109160
},
110161
],
111162
};

0 commit comments

Comments
 (0)