All files / src/plugins authenticate.js

75% Statements 9/12
40% Branches 4/10
100% Functions 2/2
75% Lines 9/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 1x   1x 1x               1x 1x 1x         1x     1x        
require('dotenv').config()
const fp = require('fastify-plugin')
 
module.exports = fp(async function (fastify, opts) {
  fastify.register(require('@fastify/jwt'), {
    secret: process.env.SECRET_KEY,
    cookie: {
      cookieName: 'token',
      signed: true
    }
  })
 
  fastify.decorate('authenticate', async function (request, reply) {
    try {
      await request.jwtVerify({ onlyCookie: true })
      if (request.url === '/welcome' || request.url === '/login' || request.url === '/register') {
        return reply.redirect(302, '/')
      }
    } catch (err) {
      Iif (request.url === '/welcome' || request.url === '/login' || request.url === '/register') {
        return err
      }
      return reply.redirect(302, '/welcome')
    }
  })
})