DeveloperBreeze

تطبيق ويب كامل Development Tutorials, Guides & Insights

Unlock 1+ expert-curated تطبيق ويب كامل tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your تطبيق ويب كامل skills on DeveloperBreeze.

بناء تطبيق ويب متقدم باستخدام React.js وNode.js

Tutorial September 27, 2024
javascript

2. إضافة مسار الدخول (Login Route):

// Login user
router.post('/login', async (req, res) => {
    const { email, password } = req.body;

    try {
        const user = await User.findOne({ email });
        if (!user) return res.status(400).json({ message: 'المستخدم غير موجود' });

        const isMatch = await bcrypt.compare(password, user.password);
        if (!isMatch) return res.status(400).json({ message: 'كلمة المرور غير صحيحة' });

        const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
        res.json({ token });
    } catch (error) {
        res.status(500).json({ message: 'خطأ في الدخول' });
    }
});