Skip to content

Commit fee1744

Browse files
chore: better default for useAsTitle with custom auth collections (#9841)
### What? Custom auth collections default `useAsTitle` to `id`. ### Why? It is more expected for auth collections to search on email or username. ### How? Defaults useAsTitle to `username` if loginWithUsername is used, else `email`. Can still be overridden by setting a custom `admin.useAsTitle` property.
1 parent 7642837 commit fee1744

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

packages/payload/src/collections/config/sanitize.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ export const sanitizeCollection = async (
195195
sanitized.auth.loginWithUsername = false
196196
}
197197

198+
if (!collection?.admin?.useAsTitle) {
199+
sanitized.admin.useAsTitle = sanitized.auth.loginWithUsername ? 'username' : 'email'
200+
}
201+
198202
sanitized.fields = mergeBaseFields(sanitized.fields, getBaseAuthFields(sanitized.auth))
199203
}
200204

test/login-with-username/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ export const LoginWithUsernameConfig = buildConfigWithDefaults({
3232
},
3333
fields: [],
3434
},
35+
{
36+
slug: 'require-email',
37+
auth: {
38+
loginWithUsername: {
39+
requireEmail: true,
40+
allowEmailLogin: false,
41+
},
42+
},
43+
fields: [],
44+
admin: {
45+
useAsTitle: 'email',
46+
},
47+
},
3548
],
3649
})
3750

test/login-with-username/payload-types.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ export interface Config {
1010
auth: {
1111
users: UserAuthOperations;
1212
'login-with-either': LoginWithEitherAuthOperations;
13+
'require-email': RequireEmailAuthOperations;
1314
};
1415
collections: {
1516
users: User;
1617
'login-with-either': LoginWithEither;
18+
'require-email': RequireEmail;
1719
'payload-locked-documents': PayloadLockedDocument;
1820
'payload-preferences': PayloadPreference;
1921
'payload-migrations': PayloadMigration;
@@ -22,6 +24,7 @@ export interface Config {
2224
collectionsSelect: {
2325
users: UsersSelect<false> | UsersSelect<true>;
2426
'login-with-either': LoginWithEitherSelect<false> | LoginWithEitherSelect<true>;
27+
'require-email': RequireEmailSelect<false> | RequireEmailSelect<true>;
2528
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
2629
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
2730
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
@@ -38,6 +41,9 @@ export interface Config {
3841
})
3942
| (LoginWithEither & {
4043
collection: 'login-with-either';
44+
})
45+
| (RequireEmail & {
46+
collection: 'require-email';
4147
});
4248
jobs: {
4349
tasks: unknown;
@@ -90,6 +96,23 @@ export interface LoginWithEitherAuthOperations {
9096
username: string;
9197
};
9298
}
99+
export interface RequireEmailAuthOperations {
100+
forgotPassword: {
101+
username: string;
102+
};
103+
login: {
104+
password: string;
105+
username: string;
106+
};
107+
registerFirstUser: {
108+
password: string;
109+
username: string;
110+
email: string;
111+
};
112+
unlock: {
113+
username: string;
114+
};
115+
}
93116
/**
94117
* This interface was referenced by `Config`'s JSON-Schema
95118
* via the `definition` "users".
@@ -126,6 +149,24 @@ export interface LoginWithEither {
126149
lockUntil?: string | null;
127150
password?: string | null;
128151
}
152+
/**
153+
* This interface was referenced by `Config`'s JSON-Schema
154+
* via the `definition` "require-email".
155+
*/
156+
export interface RequireEmail {
157+
id: string;
158+
updatedAt: string;
159+
createdAt: string;
160+
email: string;
161+
username: string;
162+
resetPasswordToken?: string | null;
163+
resetPasswordExpiration?: string | null;
164+
salt?: string | null;
165+
hash?: string | null;
166+
loginAttempts?: number | null;
167+
lockUntil?: string | null;
168+
password?: string | null;
169+
}
129170
/**
130171
* This interface was referenced by `Config`'s JSON-Schema
131172
* via the `definition` "payload-locked-documents".
@@ -140,6 +181,10 @@ export interface PayloadLockedDocument {
140181
| ({
141182
relationTo: 'login-with-either';
142183
value: string | LoginWithEither;
184+
} | null)
185+
| ({
186+
relationTo: 'require-email';
187+
value: string | RequireEmail;
143188
} | null);
144189
globalSlug?: string | null;
145190
user:
@@ -150,6 +195,10 @@ export interface PayloadLockedDocument {
150195
| {
151196
relationTo: 'login-with-either';
152197
value: string | LoginWithEither;
198+
}
199+
| {
200+
relationTo: 'require-email';
201+
value: string | RequireEmail;
153202
};
154203
updatedAt: string;
155204
createdAt: string;
@@ -168,6 +217,10 @@ export interface PayloadPreference {
168217
| {
169218
relationTo: 'login-with-either';
170219
value: string | LoginWithEither;
220+
}
221+
| {
222+
relationTo: 'require-email';
223+
value: string | RequireEmail;
171224
};
172225
key?: string | null;
173226
value?:
@@ -225,6 +278,22 @@ export interface LoginWithEitherSelect<T extends boolean = true> {
225278
loginAttempts?: T;
226279
lockUntil?: T;
227280
}
281+
/**
282+
* This interface was referenced by `Config`'s JSON-Schema
283+
* via the `definition` "require-email_select".
284+
*/
285+
export interface RequireEmailSelect<T extends boolean = true> {
286+
updatedAt?: T;
287+
createdAt?: T;
288+
email?: T;
289+
username?: T;
290+
resetPasswordToken?: T;
291+
resetPasswordExpiration?: T;
292+
salt?: T;
293+
hash?: T;
294+
loginAttempts?: T;
295+
lockUntil?: T;
296+
}
228297
/**
229298
* This interface was referenced by `Config`'s JSON-Schema
230299
* via the `definition` "payload-locked-documents_select".

0 commit comments

Comments
 (0)