Next.js Discord

Discord Forum

Next-auth callback not working on production

Answered
Yacare Caiman posted this in #help-forum
Open in Discord
Yacare CaimanOP
Can anybody help me? I'm using next-auth in a form, it should authenticate the user and then redirect him to another page. This works perfectly in development, but in production (deployed on vercel) it does not redirect the user, and if I try accessing any other page, I get redirected to the form with url "callbackUrl=%F2".

Here's a code snippet of the form, with my onSubmit function.

const onSubmit: SubmitHandler<FieldValues> = (data) => {
        setIsLoading(true)

        if (variant === 'register') {
            axios.post('/api/register', data)
                .then(() => signIn('credentials', {
                    ...data,
                    redirect: false
                }))
                .then(callback => {
                    if (callback?.error) {
                        toast.error('Invalid credentials')
                        return
                    }
                    if (callback?.ok) {
                        toast.success('Logged in!')
                        router.push('/conversations')
                    }
                })
                .catch(() => toast.error('Something went wrong!'))
            setIsLoading(false)
        }

        if (variant === 'login') {
            signIn('credentials', {
                ...data,
                redirect: false
            })
                .then(callback => {
                    if (callback?.error) {
                        toast.error('Invalid credentials')
                        return
                    }
                    if (callback?.ok) {
                        toast.success('Logged in!')
                        router.push('/conversations')
                    }
                })
            setIsLoading(false)
        }
    }
Answered by Yacare Caiman
oh yeah, it was as simple as deleting the NEXTAUTH_URL env variable in vercel
View full answer

10 Replies

@Yacare Caiman i'm having the same issue
were you able to solve it?
Yacare CaimanOP
oh yeah, it was as simple as deleting the NEXTAUTH_URL env variable in vercel
Answer
Yacare CaimanOP
it appears that Vercel manages that one itself
try it out and get back to me
wow so thats all
thanks ill try it now
Yacare CaimanOP
kk good luck!
wow 😂
you're a life saver it worked
thanks alot
been try to fix the issue for hours
Thanks again
@sheddyboy wow 😂 you're a life saver it worked thanks alot
Yacare CaimanOP
hahaha glad it worked!