Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
13
Indexable
const signUp = async (req, res) => {
    try {
      const hashedPassword = await bcrypt.hash(req.body.password, 10);
      const newUser = new User({
        ...req.body,
        password: hashedPassword
      });
      await newUser.save();
      res.json({ inserted: true });
    } catch (error) {
      res.json({ inserted: false, error: error.message });
    }
  };
  
  // Sign-in endpoint
  const signIn = async (req, res) => {
    try {
      const user = await User.findOne({ email: req.body.email });
      if (!user) return res.json({ authenticated: false, message: 'User not found' });
  
      const isPasswordValid = await bcrypt.compare(req.body.password, user.password);
      if (!isPasswordValid) return res.json({ authenticated: false, message: 'Invalid password' });
      const token = jwt.sign({ userId: user._id }, SECRET_KEY, { expiresIn: '1hr' })
      res.json({ authenticated: true, user });
    } catch (error) {
      res.json({ authenticated: false, error: error.message });
    }
  };
  
  const signUpGet = async (req, res) => {
    try {
        const users = await User.find()
        res.status(201).json(users)
        
    } catch (error) {
        res.status(500).json({ error: 'Unable to get users' })
    }
};
Editor is loading...
Leave a Comment