pbteja1998 / remix-auth-email-link

MIT License
92 stars 22 forks source link

Sending magic link successfully redirects to failureRedirect not successRedirect #3

Closed tcaer closed 2 years ago

tcaer commented 2 years ago

Hi,

So the behavior of my app works exactly as expected except for the redirects when sending the email link. Instead of redirecting to my set successRedirect I am redirected to the failureRedirect. This occurs even though my sendEmail function does not throw any exceptions or errors. I am not sure what I am doing wrong.

sendEmail:

export let sendEmail: SendEmailFunction<User> = async (options) => {
  let subject = "Log into Outryder";
  let html = renderToString(
    <p>
      Hey { options.user?.name || "there" }!<br/>
      <br />
      <a href={options.magicLink}>Click here to login.</a>
    </p>
  );

  let transporter = nodemailer.createTransport(
    nodemailerSendgrid({
      apiKey: process.env.MAGIC_LINK_API_KEY!
    })
  );

  await transporter.sendMail({
    from: "noreply@outryder.app",
    to: options.emailAddress,
    subject,
    html
  });
}

Login page:

export let loader: LoaderFunction = async ({ request }) => {
  await authenticator.isAuthenticated(request, { successRedirect: "/dashboard "});
  let session = await getSession(request.headers.get("Cookie"));

  return json({
    magicLinkSend: session.has("auth:magiclink")
  });
};

export let action: ActionFunction = async ({ request }) => {
  await authenticator.authenticate("email-link", request, {
    successRedirect: "/auth/verify",
    failureRedirect: "/auth/verify?error=true"
  });
};

export default function SignIn() {
  let { magicLinkSent } = useLoaderData<{ magicLinkSent: boolean }>()

  return (
    <Form action="/signin" method="post">
      has send email: { magicLinkSent ? "true" : "false" }.
      <div>
        <label htmlFor="email">Email Address</label>
        <input id="email" type="email" name="email" required />
      </div>
      <button type="submit">Submit</button>
    </Form>
  );
}

So I get redirected to /auth/verify?error=true. Furthermore, when I had the failureRedirect to be the same as the login link, the magicLinkSent parameter wasn't updated.

pbteja1998 commented 2 years ago

Thanks for reporting this. Someone else also reported this. When I was using it in my own project, my success redirect and failure redirect were same, so maybe that's why I didn't encounter this bug. Let me try this today and will get back to you afterwards.

pbteja1998 commented 2 years ago

@tcaer Fixed the bug. Can you try it out with the latest version (1.0.5) once and let me know if it works.

tcaer commented 2 years ago

That fixed it, thanks! I'll close the issue now.

pbteja1998 commented 2 years ago

Awesome!