const express = require('request-promise');
const crypto = require('crypto');
const fengchaoadmin = require('../db/mongo/fengchaoadmin');
const session = require('../db/mongo/session');

const _ = require('lodash');

exports.login = async (req,res,next) => {
	let {username,password} = req.body;
	console.dir(crypto.createHash('md5').update(password).digest('hex'));
	let user = await fengchaoadmin.findOne({username:username,password:crypto.createHash('md5').update(password).digest('hex')});
	console.dir(user);
	if(user){
		let sess = { user: user._id};
		const _sess = await new session(sess).save();
		res.cookie('sess',_sess._id+'');
		res.cookie('username',username);
		res.send({result: 'ok'});
	}
	else
		res.status(500).send({result:'密码不正确'});
		
};