System Information
- Strapi Version: 4.0.4
- Operating System: Windows10
- Database:
- Node Version: 16.13.2
- NPM Version: 8.1.2
- Yarn Version:
Hi all.
So I’ve set up a frontend page with Next.js and a backend with Strapi. Still running local, cause I’m not done. Then i made my login page which do a request to a frontent api and futher to the strapi api: /api/auth/local. It works with the correct user, but when i try to login with non-existing user I get a 500 error “internal server error” on client side as response from /api/auth/local, but in the terminal where I have my backend running I get “Invalid identifier or password”. How do i get that message to the client? See pictures below:
Frontend code:
import { API_URL } from '@/config/index';
export default async (req, res) => {
if(req.method === 'POST') {
const { identifier, password } = req.body;
const strapiRes = await fetch(`${API_URL}/api/auth/local`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
identifier,
password
})
})
const data = await strapiRes.json() // Returns 500 error
if(strapiRes.ok) {
//@toDo - Set Cookie
res.status(200).json({user: data.user})
} else {
if(!identifier) {
res.status(405).json({message: "Email is required to login"})
} else if(!password) {
res.status(405).json({message: "Password is required to login"})
} else {
res.status(data.error.status).json({message: data.error.message})
}
}
res.status(200).json({});
} else {
// res.setHeader('Allow', ['POST'])
res.status(405).json({message: `Method ${req.method} not allowed`});
}
}
Terminal: