FoalTS / foal

Full-featured Node.js framework 🚀
https://foalts.org/
MIT License
1.9k stars 142 forks source link

Question : static access to Context outside a controller #1230

Closed mboullouz closed 1 year ago

mboullouz commented 1 year ago

Is it possible to access the 'Context' object from outside the controllers, such as within a simple utility class like 'UiUtils'? For instance:

export class UiUtils {
    static getCurrentUsername() {
         return StaticContext.user?.email;  //StaticContext is Foal Context instance
    }
 }

The rationale (in a non-single page application): In a UI template, we require the ability to consistently display the current username, regardless of the active controller.

LoicPoullain commented 1 year ago

Hi @mboullouz 👋

Foal does not provide such feature. Maybe you can make it work with Node.JS async hooks but I'm not sure that will be easy.