import jwt from 'jsonwebtoken';
import crypto from 'crypto';
import debug from 'debug';

import User from '../../models/user';

let authLog = debug("v1/auth");

export default function authenticate(router) {
  router.post('/auth/register', (req, res) => {
    let errors = [];

    if(!req.body.email) errors.push('no email set');
    if(!req.body.username) errors.push('no username set');
    if(!req.body.password) errors.push('no password set');
    if(!req.body.passwordRepeat) errors.push('no password confirmation set');

    if(errors.length > 0) return res.status(403).json({ 'error': errors });

    if(req.body.password !== req.body.passwordRepeat) return res.status(422).json({ 'error': 'passwords don\'t match' });

    authLog(`creating new user ${req.body.email}`);

    let user = new User({ 'username': req.body.username });

    user.save(e => {
      if(e) return res.status(422).json({ 'error': e });

      authLog(`user ${user.username} created`);
      crypto.randomBytes(256, (e, buf) => {
        if(e) {
          authLog(`couldn't create random byte buffer. Err: ${e}`);
          return res.status(500).json({ 'error': 'could not create user, please try again later' });
        }
        let salt = buf.toString('hex');

        crypto.pbkdf2(req.body.password, salt, 2048, 'sha256', (e, hash) => {
          if(e) {
            authLog(`couldn't salt password. Err: ${e}`);
            return res.status(500).json({ 'error': 'could not set password, please try again later' });
          }

          let auth = new Authentication({
            'user': user.id,
            'email': req.body.email,
            'hash': hash,
            'salt': salt
          });

          auth.save(e => {
            if(e) return res.status(422).json({ 'error': e });

            authLog(`user ${user.username} successfully saved!`);
          });
        });
      });
    })
  });

  router.post('/auth/login', (req, res) => {

  });

  router.post('/auth/password/reset', (req, res) => {

  });

  router.delete('/auth/session/:userID', (req, res) => {

  });
}

@@ -124,11 +124,71 @@
 r';%0A
-%0Ale
+import Authentication from '../../models/authentication';%0A%0Acons
 t au
@@ -205,17 +205,21 @@
 bug(
-%22v1/
+'api:v1:
 auth
-%22
+'
 );%0A%0A
@@ -317,18 +317,20 @@
 %3E %7B%0A    
-le
+cons
 t errors
@@ -339,24 +339,25 @@
  %5B%5D;%0A%0A    if
+ 
 (!req.body.e
@@ -389,32 +389,33 @@
 il set');%0A    if
+ 
 (!req.body.usern
@@ -449,32 +449,33 @@
 me set');%0A    if
+ 
 (!req.body.passw
@@ -517,16 +517,17 @@
 ;%0A    if
+ 
 (!req.bo
@@ -597,16 +597,17 @@
 %0A%0A    if
+ 
 (errors.
@@ -650,27 +650,14 @@
 son(
-%7B 'error': 
 errors
- %7D
 );%0A%0A
@@ -662,16 +662,17 @@
 %0A%0A    if
+ 
 (req.bod
@@ -739,27 +739,16 @@
 2).json(
-%7B 'error': 
 'passwor
@@ -763,18 +763,16 @@
 t match'
- %7D
 );%0A%0A    
@@ -824,18 +824,20 @@
 );%0A%0A    
-le
+cons
 t user =
@@ -904,32 +904,33 @@
 (e =%3E %7B%0A      if
+ 
 (e) return res.s
@@ -941,38 +941,25 @@
 s(422).json(
-%7B 'error': e %7D
+e
 );%0A%0A      au
@@ -1044,32 +1044,33 @@
  =%3E %7B%0A        if
+ 
 (e) %7B%0A          
@@ -1161,35 +1161,24 @@
 s(500).json(
-%7B 'error': 
 'could not c
@@ -1204,34 +1204,32 @@
 try again later'
- %7D
 );%0A        %7D%0A   
@@ -1233,18 +1233,20 @@
         
-le
+cons
 t salt =
@@ -1320,16 +1320,21 @@
 t, 2048,
+ 256,
  'sha256
@@ -1359,24 +1359,25 @@
           if
+ 
 (e) %7B%0A      
@@ -1468,27 +1468,16 @@
 0).json(
-%7B 'error': 
 'could n
@@ -1516,18 +1516,16 @@
 n later'
- %7D
 );%0A     
@@ -1542,18 +1542,20 @@
         
-le
+cons
 t auth =
@@ -1666,16 +1666,32 @@
 h': hash
+.toString('hex')
 ,%0A      
@@ -1765,16 +1765,17 @@
       if
+ 
 (e) retu
@@ -1878,24 +1878,80 @@
 y saved!%60);%0A
+            return res.status(200).send(user.toJSON());%0A
           %7D)
@@ -1980,16 +1980,17 @@
 ;%0A    %7D)
+;
 %0A  %7D);%0A%0A

add working test example for register