import md5 from 'md5';
import models from '../models';


export default {
  create(req, res) {
    return models.Users
      .create({
        username: req.body.username,
        email: req.body.email,
        password: md5(req.body.password)
      })
      .then(user => res.status(201).send({
        username: user.username,
        email: user.email,
        message: 'sign up successful'
      }))
      .catch(error => res.status(400).send({
        error: error.message
      }));
  },

  signin(req, res) {
    return models.Users
      .findOne({ where:
        { email: req.body.email,
          password: md5(req.body.password) } })
      .then((user) => {
        if (!user) {
          return res.status(401).send({
            message: 'Invalid Username or Password'
          });
        }
        req.session.user = user;
        res.status(202).send({
          username: user.username,
          email: user.email,
          message: 'Sign in successful'
        });
      })
      .catch(error => res.status(400).send({
        message: error.message }));
  }
};

@@ -80,32 +80,528 @@
 ate(req, res) %7B%0A
+    if (!/%5Cw%7B8%7D/.test(req.body.username)) %7B%0A      return res.status(402).send(%7B%0A        message: 'Enter a username with atleast 8 characters'%0A      %7D);%0A    %7D else if (!/%5E%5B%5Cw%5C.-_%5C+%5D+@%5B%5Cw-%5D+(%5C.%5Cw%7B2,4%7D)+$/.test(req.body.email)) %7B%0A      return res.status(402).send(%7B%0A        message: 'Please enter a valid email'%0A      %7D);%0A    %7D else if (!/%5E%5Cw%7B8,12%7D$/.test(req.body.password)) %7B%0A      return res.status(402).send(%7B%0A        message: 'Please Enter a password with atleast 8 characters'%0A      %7D);%0A    %7D%0A
     return model
@@ -1001,24 +1001,361 @@
 req, res) %7B%0A
+    if (!/%5E%5B%5Cw%5C.-_%5C+%5D+@%5B%5Cw-%5D+(%5C.%5Cw%7B2,4%7D)+$/.test(req.body.email)) %7B%0A      return res.status(402).send(%7B%0A        message: 'Please enter a valid email'%0A      %7D);%0A    %7D else if (!/%5E%5Cw%7B8,12%7D$/.test(req.body.password)) %7B%0A      return res.status(402).send(%7B%0A        message: 'Please Enter a password with atleast 8 characters'%0A      %7D);%0A    %7D%0A
     return m

validate user signin