Giter Club home page Giter Club logo

Comments (6)

climba03003 avatar climba03003 commented on August 11, 2024

Can you provide a minimal repro for your issue? It is more straight forward to debug or find out the problem with code.

Encapsulation is creating context base on .register, folder structure do not affect the encapsulation.
I would recommend to register session plugin globally, because it is hard to determine if you have any other routes / prefix need the session when your project growth.

from fastify-secure-session.

MyTotoro avatar MyTotoro commented on August 11, 2024

@climba03003

https://github.com/MyTotoro/ExampleFastifyApp

I've posted a minimal repo generated using fastify-cli, hopefully you can easily reproduce what I'm seeing with a few commands:

  1. npm install
  2. npm run dev

the request.session works fine with /auth/ and /auth/2 routes, you can see in the log that the session data is being stored and retrieved. What I expect is that this would work across other routes / inner folder structures. In places like /example, I don't need the access to session.

But as you said the folder structure might have no impact on this. Maybe I thought this would be similar to how fastify-cors would be accessible to every route?

from fastify-secure-session.

climba03003 avatar climba03003 commented on August 11, 2024

As you are using fastify-autoload, my statement about folder is not correct. The folder structure do matter.
My advice would be split the protected and public routes into two app.js.

For example:

// app.js
'use strict'
const path = require('path')
const AutoLoad = require('fastify-autoload')
const protected = require('./context/protected')
const public = require('./context/public')

module.exports = async function (fastify, opts) {

  // plugins for global
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'plugins'),
    options: Object.assign({}, opts)
  })

  // register protected route with encapsulated context
  fastify.register(protected)
  // register public route
  fastify.register(public)
}

// context/protected.js
module.exports = async function (fastify, opts) {
  //
  fastify.register(require('fastify-secure-session'))

  // load protected routes only
}

// context/public.js
module.exports = async function (fastify, opts) {
  // load public routes only
}

from fastify-secure-session.

MyTotoro avatar MyTotoro commented on August 11, 2024

what documentation describes how fastify-autoload manages context? I'd like to properly understand whats happening

Thanks for the suggestion. I don't quite understand how registering fastify-secure-session in context/protected is different than my folder structure? Unless the fastify instance that is passed into context/protected routes will have context properly populated with secure-session?

from fastify-secure-session.

climba03003 avatar climba03003 commented on August 11, 2024

fastify-autoload is a plugin to help you do .register automatically. Unless you explicitly specified, each .register should create a new encapsulate context.

I am against any starter using fastify-autoload as it hide all the detail of how fastify runs and how to deal with encapsulation.

from fastify-secure-session.

MyTotoro avatar MyTotoro commented on August 11, 2024

I see - I read more into the documentation of autoload, I just moved the auth logic into a plugin and registered as a plugin across my app. Thanks for your help

from fastify-secure-session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.