Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
strapi returns Internal Server Error though postman delivers correct response

I'm setting up an authentication flow with JWT with my strapi backend and a next.js frontend. Testing the backend with postman works as expected. I get back a user object and a JWT token. But calling my backend from my frontend results in a statuscode 500 - internal server error. I honestly don't see where I'm wrong and I tried to rewrite those calls from scratch again and again. Does anyone see where I have been wrong?

Here's my register.js where the call to my backend happens:

import cookie from 'cookie'
import { API_URL } from '@/config/index'

export default async (req, res) => {
    if (req.method === 'POST') {
        const { username, email, password } = req.body

        const strapiRes = await fetch(`${API_URL}/auth/local/register`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username,
                email,
                password,
            }),
        })

        const data = await strapiRes.json()

        if (strapiRes.ok) {
            // Set Cookie
            res.setHeader(
                'Set-Cookie',
                cookie.serialize('token', data.jwt, {
                    httpOnly: true,
                    secure: process.env.NODE_ENV !== 'development',
                    maxAge: 60 * 60 * 24 * 7, // 1 week
                    sameSite: 'strict',
                    path: '/',
                })
            )

            res.status(200).json({ user: data.user })
        } else {
            res
                .status(data.statusCode)
                .json({ message: data.message[0].messages[0].message })
        }
    } else {
        res.setHeader('Allow', ['POST'])
        res.status(405).json({ message: `Method ${req.method} not allowed` })
    }

This is my register function in my context

const register = async (user) => {
        const res = await fetch(`${NEXT_CLIENT_URL}/api/register`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(user),
        })

        const data = await res.json()

        if (res.ok) {
            setUser(data.user)
            router.push('/account/dashboard')
        } else {
            setError(data.message)
            setError(null)
        }
    }

This is how I call the context function in my next frontend

 const handleSubmit = (e) => {
        e.preventDefault()

        if (password !== password2) {
            toast.error('Passwords do not match!')
            return
        }

        register({ username, email, password })
    }

Glad for every piece of advice! Logging in works as expected in the same app.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda