I am creating a custom endpoint for resendEmailVerification
that can be called in a client side using trpc
.
1. This is my code on customresendEmailVerification
endpoint:
{
const verificationTokenData = await ctx.prisma.verificationToken.create({
data: {
identifier: email,
token: crypto.randomBytes(32).toString("hex"),
expires: new Date(Date.now() + 1000 * 60 * 60 * 24), // expires in 24 hours
},
});
console.log(verificationTokenData) // Loggin Here.
const url = `http://localhost:3000/api/auth/callback/email?callbackUrl=${encodeURIComponent(
"http://localhost:3000/profile"
)}&token=${verificationTokenData.token}&email=${encodeURIComponent(
verificationTokenData.identifier
)}`;
const server = {
host: env.EMAIL_SERVER_HOST,
port: parseInt(env.EMAIL_SERVER_PORT),
auth: {
user: env.EMAIL_SERVER_USER,
pass: env.EMAIL_SERVER_PASSWORD,
},
};
const { host } = new URL(url);
const transport = createTransport(server);
try {
const result = await transport.sendMail({
to: email,
from: env.EMAIL_FROM,
subject: `Sign in to ${host}`,
text: text({ url, host }),
html: html({ url, host }),
});
const failed = result.rejected.concat(result.pending).filter(Boolean);
if (failed.length) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Email(s) (${failed.join(", ")}) could not be sent`,
});
}
return { message: "Verification email sent." };
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to send verification email.",
});
}
}
2. When I call this part from the client-side I am sending the following data:
{
identifier: '[email protected]',
token: '35c327ca7698f907c3400f19f5a02b48494070bc9ed6b1bdcd241baf31b4e592',
expires: 2023-06-23T10:24:13.633Z
}
3 Which are correctly sent via email with a redirect URL:
http://localhost:3000/api/auth/callback/email?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fprofile&token=35c327ca7698f907c3400f19f5a02b48494070bc9ed6b1bdcd241baf31b4e592&email=testemail%40gmail.com
4 However, once I click this redirect link, my server receives this data in async useVerificationToken(identifier_token)
inside @next-auth/prisma-adapter/dist/index.js
:
{
identifier: '[email protected]',
token: '1fe03f64155ca194a9c9997f43b3655ce8ca6b9436aa3e5ebd2e4a60a5b04bba'
}
Question: What could be the cause of this change of token values??
client's sent token:
35c327ca7698f907c3400f19f5a02b48494070bc9ed6b1bdcd241baf31b4e592
server's received token:
1fe03f64155ca194a9c9997f43b3655ce8ca6b9436aa3e5ebd2e4a60a5b04bba
I am assuming there is a certain logic when the server receives the token from the client. I would like to know it so that I can complete my custom resendEmailVerification
functionality.
Thanks.