Skip to content

Magic Link doesn't work for registration.  #2

@RomanDenysov

Description

@RomanDenysov

Description:
The issue arises when attempting to register a new user using the magic link functionality in a custom PayloadCMS plugin. The expected behavior is that the user receives a magic link and can use it to register. However, the current implementation fails to register new users successfully via magic link.

Problem:
The problem lies in how the current code handles the magic link verification and user lookup. Specifically:

  • In the createVerificationToken function, the code assumes that the user already exists in the system by looking them up via their email. If the user is not found (if (!payloadUser)), the function returns null. This behavior prevents new users from being registered using the magic link, as it expects users to already be in the system.
  • There is no logic to handle the case where the user doesn't exist and should be created as part of the registration flow.

Steps to Reproduce:

  1. Attempt to register a new user by generating a magic link for an email that does not yet exist in the system.
  2. Follow the link and verify the token.

Expected Behavior:

  • If the email does not exist in the system, the user should be created during the magic link verification process.
  • The token should be sent to the new user's email, allowing them to register upon clicking the magic link.

Actual Behavior:

  • If the email is not found, the registration process fails because the system does not create new users as part of the magic link registration flow.
  • The function createVerificationToken returns null without creating a new user.

Suggested Fix:

  • Modify the logic in the createVerificationToken function to handle the case where the user does not yet exist in the system. If the user does not exist, create a new user and associate the token with them.
  • Ensure that the useVerificationToken function handles both existing and new users, completing the registration process for new users who use the magic link.
async createVerificationToken({ identifier: email, ...token }) {
  let payloadUser = (
    await (
      await payload
    ).find({
      collection: userCollectionSlug,
      where: {
        email: {
          equals: email,
        },
      },
    })
  ).docs.at(0) as User | undefined;

  // If user does not exist, create a new one
  if (!payloadUser) {
    payloadUser = await (
      await payload
    ).create({
      collection: userCollectionSlug,
      data: {
        email,
        verificationTokens: [token],
        // Any other necessary fields
      },
    });
  } else {
    payloadUser = (await (
      await payload
    ).update({
      collection: userCollectionSlug,
      id: payloadUser.id,
      data: {
        verificationTokens: [...(payloadUser.verificationTokens || []), token],
      },
    })) as User;
  }

  return {
    identifier: email,
    ...token,
  };
}

This change ensures that a new user is created if they don't already exist, allowing the magic link registration to work as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions