User sessions — Create a Connect 4 online training

Now that we have gone through the front-end part, we will be able to attack the server part, starting with the user session system. For the server we will use Fastify and we will manage sessions stateless without using a database.

When the user contacts the server, he will be assigned a unique identifier and a signature that will be created using a private key. To manage this cryptography part we will rely on the NodeJS crypto API

import Fastify from 'fastify'
import { v4 } from 'uuid'
import { sign } from './func/crypto'

const fastify = Fastify({logger: true})

fastify.post('/api/players', (req, res) => {
  const playerId = v4() // On génère un id unique à la volée
  const signature = sign(playerId)
  res.send({
    id: playerId,
    signature: signature
  })
})

fastify.listen({port: 8000}).catch((err) => {
  fastify.log.error(err)
  process.exit(1)
}).then(() => {
  fastify.log.info('Le serveur écoute sur le port 8000')
})