Skip to content

Commit b8c24b5

Browse files
committed
chore(dev): Allow login with username for local strategy
1 parent a0d1ab9 commit b8c24b5

File tree

8 files changed

+68
-24
lines changed

8 files changed

+68
-24
lines changed

packages/dev/src/auth/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import payloadConfig from "@payload-config";
22
import NextAuth from "next-auth";
3+
import type { DiscordProfile } from "next-auth/providers/discord";
4+
import type { GitHubProfile } from "next-auth/providers/github";
35
import { getPayload } from "payload";
46
import { withPayload } from "payload-authjs";
57
import { nodeAuthConfig } from "./node.config";
@@ -11,14 +13,28 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
1113
/**
1214
* Update user on every sign in
1315
*/
14-
signIn: async ({ adapter, user, profile }) => {
16+
signIn: async ({ adapter, user, account, profile }) => {
1517
if (!user.id || !profile) {
1618
return;
1719
}
1820
await adapter.updateUser!({
1921
id: user.id,
20-
name: profile.name ?? (profile.login as string | undefined),
21-
image: profile.avatar_url as string | undefined,
22+
...(account?.provider === "github" && {
23+
name:
24+
(profile as unknown as GitHubProfile).name ??
25+
(profile as unknown as GitHubProfile).login,
26+
email: profile.email ?? undefined,
27+
image: (profile as unknown as GitHubProfile).avatar_url,
28+
}),
29+
...(account?.provider === "keycloak" && {
30+
name: profile.name,
31+
email: profile.email ?? undefined,
32+
}),
33+
...(account?.provider === "discord" && {
34+
name: (profile as unknown as DiscordProfile).global_name,
35+
email: profile.email ?? undefined,
36+
image: (profile as unknown as DiscordProfile).image_url,
37+
}),
2238
additionalUserDatabaseField: `Create by signIn event at ${new Date().toISOString()}`,
2339
});
2440
},

packages/dev/src/auth/providers/discord.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const discordProvider = discord({
1010
// Default fields from discord provider
1111
...(await discord({}).profile!(profile, tokens)),
1212
// Custom fields
13+
username: profile.username,
1314
additionalUserDatabaseField: `Create by discord provider profile callback at ${new Date().toISOString()}`,
1415
};
1516
},

packages/dev/src/auth/providers/github.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ export const githubProvider = github({
55
/**
66
* Add additional fields to the user on first sign in
77
*/
8-
profile(profile) {
8+
async profile(profile, tokens) {
99
return {
10-
// Default fields (@see https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/github.ts#L176)
11-
id: profile.id.toString(),
12-
name: profile.name ?? profile.login,
13-
email: profile.email,
14-
image: profile.avatar_url,
10+
// Default fields from github provider
11+
...(await github({}).profile!(profile, tokens)),
1512
// Custom fields
13+
username: profile.login,
1614
additionalUserDatabaseField: `Create by github provider profile callback at ${new Date().toISOString()}`,
1715
};
1816
},

packages/dev/src/auth/providers/keycloak.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const keycloakProvider = keycloak({
1313
email: profile.email,
1414
image: profile.picture,
1515
// Custom fields
16+
username: profile.preferred_username,
1617
locale: profile.locale,
1718
additionalUserDatabaseField: `Create by keycloak provider profile callback at ${new Date().toISOString()}`,
1819
};

packages/dev/src/payload-types.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,34 @@ export interface Config {
9696
};
9797
}
9898
export interface UserAuthOperations {
99-
forgotPassword: {
100-
email: string;
101-
password: string;
102-
};
103-
login: {
104-
email: string;
105-
password: string;
106-
};
99+
forgotPassword:
100+
| {
101+
email: string;
102+
}
103+
| {
104+
username: string;
105+
};
106+
login:
107+
| {
108+
email: string;
109+
password: string;
110+
}
111+
| {
112+
password: string;
113+
username: string;
114+
};
107115
registerFirstUser: {
108-
email: string;
109116
password: string;
110-
};
111-
unlock: {
117+
username?: string;
112118
email: string;
113-
password: string;
114119
};
120+
unlock:
121+
| {
122+
email: string;
123+
}
124+
| {
125+
username: string;
126+
};
115127
}
116128
/**
117129
* This interface was referenced by `Config`'s JSON-Schema
@@ -158,6 +170,7 @@ export interface User {
158170
enableAPIKey?: boolean | null;
159171
apiKey?: string | null;
160172
apiKeyIndex?: string | null;
173+
username?: string | null;
161174
resetPasswordToken?: string | null;
162175
resetPasswordExpiration?: string | null;
163176
salt?: string | null;
@@ -281,6 +294,7 @@ export interface UsersSelect<T extends boolean = true> {
281294
enableAPIKey?: T;
282295
apiKey?: T;
283296
apiKeyIndex?: T;
297+
username?: T;
284298
resetPasswordToken?: T;
285299
resetPasswordExpiration?: T;
286300
salt?: T;

packages/dev/src/payload.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,10 @@ export default buildConfig({
6565
},
6666
}),
6767
sharp,
68-
plugins: [authjsPlugin({ authjsConfig: nodeAuthConfig, enableLocalStrategy: true })],
68+
plugins: [
69+
authjsPlugin({
70+
authjsConfig: nodeAuthConfig,
71+
enableLocalStrategy: true,
72+
}),
73+
],
6974
});

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const Users: CollectionConfig<"users"> = {
1515
auth: {
1616
useAPIKey: true,
1717
depth: 0,
18+
loginWithUsername: {
19+
allowEmailLogin: true,
20+
requireEmail: true,
21+
requireUsername: false,
22+
},
1823
},
1924
defaultPopulate: {
2025
id: true,
@@ -24,7 +29,7 @@ const Users: CollectionConfig<"users"> = {
2429
{
2530
name: "name",
2631
type: "text",
27-
label: "Username", // Add label to name field
32+
label: "Display-Name", // Add label to name field
2833
},
2934
{
3035
name: "id",

packages/payload-authjs/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
492492
}
493493
await adapter.updateUser!({
494494
id: user.id,
495-
name: profile.name ?? (profile.login as string | undefined),
495+
name:
496+
(profile as unknown as GitHubProfile).name ??
497+
(profile as unknown as GitHubProfile).login,
498+
email: profile.email ?? undefined,
499+
image: (profile as unknown as GitHubProfile).avatar_url,
496500
});
497501
},
498502
},

0 commit comments

Comments
 (0)