dearmannerism
dearmannerism
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
So I should create a custom page instead huh
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
All of it is handled by next auth.
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
Once the user clicks on the link it is processed by next auth
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
This is my overall question which I asked in next-auth community as well. https://github.com/nextauthjs/next-auth/discussions/7860
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
resendVerificationEmail: protectedProcedure
.input(z.object({ email: z.string().email() }))
.mutation(async ({ ctx, input: { email } }) => {
const verificationToken = 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
},
});

const url = `http://localhost:3000/api/auth/callback/email?callbackUrl=${encodeURIComponent(
"http://localhost:3000/profile"
)}&token=${verificationToken.token}&email=${encodeURIComponent(
verificationToken.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.",
});
}
}),
resendVerificationEmail: protectedProcedure
.input(z.object({ email: z.string().email() }))
.mutation(async ({ ctx, input: { email } }) => {
const verificationToken = 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
},
});

const url = `http://localhost:3000/api/auth/callback/email?callbackUrl=${encodeURIComponent(
"http://localhost:3000/profile"
)}&token=${verificationToken.token}&email=${encodeURIComponent(
verificationToken.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.",
});
}
}),
this is what I got so far. I was able to send custom email, but once I click on the redirect URL I get redirected to this error page
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
It seems like that very logic of sending email verification using next-auth is hidden inside its ‘signIn(“email”, {email})’ function.
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
How can I do that??
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
This is exactly what I want to do
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
Im using next-auth for sign in and sign out. How can I possible expose next auth’s sendVerification() to my client with a protectedProcedure??
29 replies
TTCTheo's Typesafe Cult
Created by dearmannerism on 6/21/2023 in #questions
re-send email verification endpoint
Sorry for confusion.
29 replies