const router = require('express').Router()
const jwt = require('jsonwebtoken')
const auth = require('../auth')
const mail = require('../../util/mail')
const uuid = require('node-uuid')

let User
require('../../database')
.then(db => {
	User = db.model.user
})

router.get('/me', auth.ensure, (req, res) => {
	const email = req.decoded.email
	User.findOne({
		where: {
			email: email
		}
	}).then(user => {
		if (!user) {
			return res.status(500).end()
		}
		return res.json(user)
	})
})

router.get('/me/photo', auth.ensure, (req, res) => {
	res.status(501).json({photo: 'dummy photo'})
})

// --------- PUBLIC PATHS -----------
// register new user
router.post('/', (req, res) => {
	const creds = req.body
	const user = {
		email: creds.username,
		password: creds.password,
		verifiedEmail: false,
		emailToken: uuid.v4(),
		emailTokenExpires: new Date().setDate(new Date().getDate() + 1)
	}

	User.create(user)
	.then((user) => {
		mail.sendVerification(user)
		.then(data => {
			console.log(data)
		})
		.catch(err => {
			console.log(err)
		})

		if(!process.env.JWT_SECRET) throw new Error('JWT_SECRET is not set')
		const token = jwt.sign({email: user.email}, process.env.JWT_SECRET)

		return res.json({token})
	})
	.catch(err => {
		return res.status(400).send({error: err.message})
	})
})

router.get('/confirm/:token', (req, res) => {
	const token = req.params.token

	User.findOne({
		where: {
			emailToken: token
		}
	}).then(user => {
		if (!user){
			return res.status(401).send('no user associated with verification')
		}

		const expires = new Date(user.emailTokenExpires)
		const now = new Date()

		if (now > expires){
			return res.status(410).send('this link has expired, request a new link <a href="http://localhost:6001/refresh-verification">here</a>')
		}

		User.update({
			verifiedEmail: true,
			emailToken: null,
			emailTokenExpires: null
		}, {
			where: { id: user.id }
		}).then(() => {
			return res.status(200).send('thank you for verifying')
		}).catch(() => {
			return res.status(500).send('failed to update user')
		})
	})
})

router.post('/login', (req, res) => {
	const credentials = req.body,
		username = credentials.username,
		password = credentials.password

	User.findOne({
		where: {
			email: username
		}
	}).then(user => {
		if (!user) {
			return res.status(401).json({error: 'username does not exist'})
		}

		if (!user.validPassword(password)) {
			return res.status(401).json({error: 'incorrect password'})
		}

		if(!process.env.JWT_SECRET) throw new Error('JWT_SECRET is not set')
		const token = jwt.sign({email: user.email}, process.env.JWT_SECRET)

		return res.json({token})

	}, err => {
		return res.status(500).send(err)
	})
})

module.exports = router

@@ -930,16 +930,23 @@
  =%3E %7B%0A%09%09
+return 
 mail.sen
@@ -970,94 +970,25 @@
 r)%0A%09
+%7D)%0A
 %09.then(
-data =%3E %7B%0A%09%09%09console.log(data)%0A%09%09%7D)%0A%09%09.catch(err =%3E %7B%0A%09%09%09console.log(err)%0A%09%09%7D)%0A
+() =%3E %7B
 %0A%09%09i
@@ -1117,33 +1117,32 @@
 env.JWT_SECRET)%0A
-%0A
 %09%09return res.jso
@@ -1156,16 +1156,16 @@
 n%7D)%0A%09%7D)%0A
-
 %09.catch(
@@ -1165,32 +1165,53 @@
 .catch(err =%3E %7B%0A
+%09%09console.error(err)%0A
 %09%09return res.sta

Clean up promise handling