text: import Seller from '../models/seller.model'
  import jwt from 'jsonwebtoken'
  import expressJwt from 'express-jwt'
  import config from './../../config/config'
  
  const signin = async (req, res) => {
    try {
      let seller = await Seller.findOne({
        "email": req.body.email
      })
      if (!seller)
        return res.status('401').json({
          error: "Seller not found"
        })
  
      if (!seller.authenticate(req.body.password)) {
        return res.status('401').send({
          error: "Email and password don't match."
        })
      }
  
      const token = jwt.sign({
        _id: seller._id
      }, config.jwtSecret)
  
      res.cookie("t", token, {
        expire: new Date() + 9999
      })
  
      return res.json({
        token,
        seller: {
          _id: seller._id,
          name: seller.name,
          email: seller.email
        }
      })
  
    } catch (err) {
  
      return res.status('401').json({
        error: "Could not sign in"
      })
  
    }
  }
  
  const signout = (req, res) => {
    res.clearCookie("t")
    return res.status('200').json({
      message: "signed out"
    })
  }
  
  const requireSignin = expressJwt({
    secret: config.jwtSecret,
    userProperty: 'auth'
  })
  
  const hasAuthorization = (req, res, next) => {
    const authorized = req.profile && req.auth && req.profile._id == req.auth._id
    if (!(authorized)) {
      return res.status('403').json({
        error: "Seller is not authorized"
      })
    }
    next()
  }
  
  export default {
    signin,
    signout,
    requireSignin,
    hasAuthorization
  }
  