Replies: 2 comments
-
I feel like this is a LOT closer. I still pulled the hashing util in, but it's not really needed and I'm effectively using my own Thanks @goshacmd! import {NextAuthOptions} from 'next-auth'
import {createHash, randomBytes} from 'crypto'
const baseUrl = process.env.NEXTAUTH_URL || process.env.VERCEL_URL
function hashToken(token: string, options: any) {
const {provider, secret} = options
return (
createHash('sha256')
// Prefer provider specific secret, but use default secret if none specified
.update(`${token}${provider.secret ?? secret}`)
.digest('hex')
)
}
export async function sendServerEmail({
email,
callbackUrl,
nextAuthOptions,
}: {
email: string
callbackUrl?: string
nextAuthOptions: NextAuthOptions
}) {
const emailProvider: any = nextAuthOptions.providers.find(
(provider) => provider.id === 'email',
)
callbackUrl = (callbackUrl || baseUrl) as string
const identifier = email
const token =
(await emailProvider.generateVerificationToken?.()) ??
randomBytes(32).toString('hex')
const ONE_DAY_IN_SECONDS = 86400
const expires = new Date(
Date.now() + (emailProvider.maxAge ?? ONE_DAY_IN_SECONDS) * 1000,
)
await nextAuthOptions?.adapter?.createVerificationToken?.({
identifier,
token: hashToken(token, {
provider: emailProvider,
secret: nextAuthOptions.secret,
}),
expires,
})
console.log(emailProvider)
const params = new URLSearchParams({callbackUrl, token, email: identifier})
const _url = `${baseUrl}/api/auth/callback/${emailProvider.id}?${params}`
await emailProvider.sendVerificationRequest({
identifier,
url: _url,
provider: emailProvider.options,
token: token as string,
expires,
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you @joelhooks ! Interested in an officially supported solution as well. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
For our app we need to send a new user a magic link after they make a purchase in response to a Stripe webhook. The problem I ran into was that sending an email generally seems to assume we are in a browser environment with access to cookie headers, and that's not the case when stripe sends a webhook to our api route function.
To make this happen, we are manually calling the
/api/auth/csrf
route and posting to/api/auth/signin/email
Of course when stripe posts to an api endpoint, there is no cookie!
I tried to import the utils and manually create the cookie header inline, but they throw an error when I try to import them from
next-auth
so I cut and pasted them into my module.Would love feedback on this if you've got it. It works as is, but I particularly don't care for the copypasta (at all) and assume there's a much better way to accomplish this that I haven't discovered yet 😅
Beta Was this translation helpful? Give feedback.
All reactions