const jwt = require('jsonwebtoken');
const uuid = require('uuid/v4');
const routes = require('express').Router();
const { sendJSON } = require('next/dist/server/render');

routes.get('/user', (req, res) => {
  const message = '/test05';
  sendJSON(res, { message });
});

routes.post('/user', (req, res) => (
  sendJSON(res, { user: createUser() })
));

routes.post('/session', (req, res) => {
  const user = createUser();
  const token = createToken(user);
  sendJSON(res, { user, token });
});

module.exports = routes;

const createUser = () => ({
  identifier: uuid(),
});

const createToken = user => jwt.sign(
  user, process.env.SECRET_KEY_BASE, { expiresIn: '10y' }
);

@@ -161,24 +161,216 @@
 /render');%0A%0A
+const %7B createConnection %7D = require('../db');%0Aconst %7B userSchema %7D = require('../db/models/user');%0A%0Aconst connection = createConnection();%0Aconst User = connection.model('User', userSchema);%0A%0A
 routes.get('
@@ -562,16 +562,22 @@
 ession',
+ async
  (req, r
@@ -597,61 +597,214 @@
 nst 
+%7B identifier %7D = req.body;%0A  const q
 u
-s
 er
+y
  = 
-createUser();%0A  const token = createToken(user
+identifier == null ? createUser() : %7B identifier %7D;%0A  const %7B user %7D = await User.firstOrCreate(query);%0A  const token = createToken(%7B identifier: user.identifier %7D
 );%0A 

add create session