backend-login
const login = async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(404).json({ error: 'User NOT FOUND!' });
}
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) {
return res.status(401).json({ error: 'Password is not valid!' });
}
return res.status(200).json({ message: 'User logged in successfully!', user });
} catch (error) {
console.error("Error at creating user", error);
return res.status(500).json({ error: 'Internal Server ERROR' });
}
};
şimdi frontendde yakalamaya çalışıyorum
server/api/user/login.ts
export default defineEventHandler(async (event) => {
try {
const body = await readBody(event);
const user = await $fetch('http://localhost:5000/api/v1/auth/login', {
method: 'POST',
body,
headers: {
'Content-Type': 'application/json'
}
});
return user;
} catch (error) {
return {
status: error.status || 500,
body: { error: error.data?.error || 'Unknown error' }
};
}
})
store/authStore.ts
async login(newUser) {
try {
const data = await $fetch('/api/user/login', {
method: 'POST',
body: JSON.stringify(newUser),
headers: {
'Content-Type': 'application/json'
}
});
this.user = data;
console.log(data);
} catch (error) {
console.error('Error in login:', error.message || error);
throw error;
// console.log('Error message:', error.message || error.response?.data?.error || error);
// throw new Error(error.message || error.response?.data?.error || 'Unknown error');
}
}
login.vue
const submitForm = async () => {
try {
await authStore.login(formData);
console.log("Login successful");
} catch (error) {
console.log(error);
// console.error("Login failed:", error.message || error.response?.data?.error || error);
console.error("Login failed:", error.message || error);
}
};
fakat diyelim ben emaili doğru yazdım fakat parolayı yanlış bana
{
"status": 401,
"body": {
"error": "Password is not valid!"
}
}
böyle hata dönüyor fakat Login successful
dönüyor.
const submitForm = async () => {
try {
await authStore.login(formData);
console.log("Login successful");
} catch (error) {
console.log(error);
// console.error("Login failed:", error.message || error.response?.data?.error || error);
console.error("Login failed:", error.message || error);
}
};
yani catch
e gitmiyor try e gidiyor bunu nasıl çözebilirim.
Ayrıca terminalda da bana
ERROR [unhandledRejection] read ECONNRESET 11:50:00 AM
at TCP.onStreamRead (node:internal/stream_base_commons:218:20)
hata dönüyor.