Hi, Here’s my code for an implementation changing a password, (below the error message), I’m using bcrypt.compare()
This is working and the password is being changed, however, I’m getting this error message in the console, I’ve googled but I’d not getting anything re what it’s complaining about, anyone have any ideas?:
Error: No response is returned from route handler 'C:\Users\char5\OneDrive\Web_Projects\2023\next-app\app\api\resetPassword\route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.
at C:\Users\char5\OneDrive\Web_Projects\2023\next-app\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:61631
here is the route.ts for resetPassword:
import { NextRequest, NextResponse } from 'next/server';
import schema from '../resetPassword/schema';
import prisma from '@/prisma/client';
import bcrypt from 'bcrypt';
export async function PUT(
request: NextRequest
) {
const body = await request.json();
const validation = schema.safeParse(body);
if (!validation.success)
return NextResponse.json(validation.error.errors, { status: 400 });
const user = await prisma.user.findUnique({
where: { email: body.email },
});
if (!user)
return NextResponse.json({ error: 'User not found' }, { status: 404 });
const hashedPasswordFromDB = user.hashedPassword;
const passwordCompare = bcrypt.compare(
body.oldPassword,
hashedPasswordFromDB!,
async function (err, result) {
if (err)
return NextResponse.json({ error: err.message }, { status: 500 });
if (result) {
console.log('passwords match');
const newPasswordProvidedAndHashed = await bcrypt.hash(
body.newPassword,
10
);
const updateUserPassword = await prisma.user.update({
where: { id: user.id },
data: {
email: body.email,
hashedPassword: newPasswordProvidedAndHashed,
},
});
return NextResponse.json(updateUserPassword, { status: 200 });
} else {
console.log("passwords don't match");
return NextResponse.json(
{ error: 'Passwords dont match' },
{ status: 404 }
);
}
}
);
}