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.
This commit is contained in:
Jarrod Flesch
2024-12-10 08:42:23 -05:00
committed by GitHub
parent 76428373e4
commit fee17448e7
3 changed files with 86 additions and 0 deletions

View File

@@ -195,6 +195,10 @@ export const sanitizeCollection = async (
sanitized.auth.loginWithUsername = false sanitized.auth.loginWithUsername = false
} }
if (!collection?.admin?.useAsTitle) {
sanitized.admin.useAsTitle = sanitized.auth.loginWithUsername ? 'username' : 'email'
}
sanitized.fields = mergeBaseFields(sanitized.fields, getBaseAuthFields(sanitized.auth)) sanitized.fields = mergeBaseFields(sanitized.fields, getBaseAuthFields(sanitized.auth))
} }

View File

@@ -32,6 +32,19 @@ export const LoginWithUsernameConfig = buildConfigWithDefaults({
}, },
fields: [], fields: [],
}, },
{
slug: 'require-email',
auth: {
loginWithUsername: {
requireEmail: true,
allowEmailLogin: false,
},
},
fields: [],
admin: {
useAsTitle: 'email',
},
},
], ],
}) })

View File

@@ -10,10 +10,12 @@ export interface Config {
auth: { auth: {
users: UserAuthOperations; users: UserAuthOperations;
'login-with-either': LoginWithEitherAuthOperations; 'login-with-either': LoginWithEitherAuthOperations;
'require-email': RequireEmailAuthOperations;
}; };
collections: { collections: {
users: User; users: User;
'login-with-either': LoginWithEither; 'login-with-either': LoginWithEither;
'require-email': RequireEmail;
'payload-locked-documents': PayloadLockedDocument; 'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference; 'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration; 'payload-migrations': PayloadMigration;
@@ -22,6 +24,7 @@ export interface Config {
collectionsSelect: { collectionsSelect: {
users: UsersSelect<false> | UsersSelect<true>; users: UsersSelect<false> | UsersSelect<true>;
'login-with-either': LoginWithEitherSelect<false> | LoginWithEitherSelect<true>; 'login-with-either': LoginWithEitherSelect<false> | LoginWithEitherSelect<true>;
'require-email': RequireEmailSelect<false> | RequireEmailSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>; 'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>; 'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>; 'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
@@ -38,6 +41,9 @@ export interface Config {
}) })
| (LoginWithEither & { | (LoginWithEither & {
collection: 'login-with-either'; collection: 'login-with-either';
})
| (RequireEmail & {
collection: 'require-email';
}); });
jobs: { jobs: {
tasks: unknown; tasks: unknown;
@@ -90,6 +96,23 @@ export interface LoginWithEitherAuthOperations {
username: string; username: string;
}; };
} }
export interface RequireEmailAuthOperations {
forgotPassword: {
username: string;
};
login: {
password: string;
username: string;
};
registerFirstUser: {
password: string;
username: string;
email: string;
};
unlock: {
username: string;
};
}
/** /**
* This interface was referenced by `Config`'s JSON-Schema * This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users". * via the `definition` "users".
@@ -126,6 +149,24 @@ export interface LoginWithEither {
lockUntil?: string | null; lockUntil?: string | null;
password?: string | null; password?: string | null;
} }
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "require-email".
*/
export interface RequireEmail {
id: string;
updatedAt: string;
createdAt: string;
email: string;
username: string;
resetPasswordToken?: string | null;
resetPasswordExpiration?: string | null;
salt?: string | null;
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
password?: string | null;
}
/** /**
* This interface was referenced by `Config`'s JSON-Schema * This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents". * via the `definition` "payload-locked-documents".
@@ -140,6 +181,10 @@ export interface PayloadLockedDocument {
| ({ | ({
relationTo: 'login-with-either'; relationTo: 'login-with-either';
value: string | LoginWithEither; value: string | LoginWithEither;
} | null)
| ({
relationTo: 'require-email';
value: string | RequireEmail;
} | null); } | null);
globalSlug?: string | null; globalSlug?: string | null;
user: user:
@@ -150,6 +195,10 @@ export interface PayloadLockedDocument {
| { | {
relationTo: 'login-with-either'; relationTo: 'login-with-either';
value: string | LoginWithEither; value: string | LoginWithEither;
}
| {
relationTo: 'require-email';
value: string | RequireEmail;
}; };
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;
@@ -168,6 +217,10 @@ export interface PayloadPreference {
| { | {
relationTo: 'login-with-either'; relationTo: 'login-with-either';
value: string | LoginWithEither; value: string | LoginWithEither;
}
| {
relationTo: 'require-email';
value: string | RequireEmail;
}; };
key?: string | null; key?: string | null;
value?: value?:
@@ -225,6 +278,22 @@ export interface LoginWithEitherSelect<T extends boolean = true> {
loginAttempts?: T; loginAttempts?: T;
lockUntil?: T; lockUntil?: T;
} }
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "require-email_select".
*/
export interface RequireEmailSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
email?: T;
username?: T;
resetPasswordToken?: T;
resetPasswordExpiration?: T;
salt?: T;
hash?: T;
loginAttempts?: T;
lockUntil?: T;
}
/** /**
* This interface was referenced by `Config`'s JSON-Schema * This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents_select". * via the `definition` "payload-locked-documents_select".