payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.6k stars 1.51k forks source link

Type signature mismatch between auth email configurations #6953

Closed tomparkp closed 3 months ago

tomparkp commented 3 months ago

Link to reproduction

No response

Payload Version

3.0.0-beta.54

Node Version

18

Next.js Version

15

Describe the Bug

I've noticed a discrepancy between the type signatures of GenerateForgotPasswordEmailHTML and GenerateVerifyEmailHTML that seems like it might be unintentional:

type GenerateVerifyEmailHTML = (args: {
    req: PayloadRequestWithData;
    token: string;
    user: any;
}) => Promise<string> | string;

type GenerateForgotPasswordEmailHTML = (args?: {
    req?: PayloadRequestWithData;
    token?: string;
    user?: unknown;
}) => Promise<string> | string;

Based on the docs you would implement a custom Forgot Password email via user.email but user is both optional and unknown vs non-optional and any in the verify email signature. I'm addressing it by using a type guard and throwing an error if the email isn't present (not necessary for the verify email given the difference in signature).

Additionally this is more of a feature request but many of the types within Payload such as these could be improved with generics e.g. type GenerateForgotPasswordEmailHTML<UserType = unknown> so consumers can supply the TS definition for the user record. Ideally that could be taken a step further and the types could be automatically inferred from the collection definition itself, but I understand Typescript gymnastics can get complicated and that would be a bigger effort.

Reproduction Steps

  1. Setup a Payload beta project with user collection
  2. Attempt to add custom email functions for verify and forgot password
  3. Note the difference in the type signatures

Adapters and Plugins

No response

paulpopus commented 3 months ago

Changed those to be a generic that can be passed down, will be available in the next beta release some time this week

github-actions[bot] commented 3 weeks ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.