FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.88k stars 137 forks source link

Question : static access to Context outside a controller #1230

Closed mboullouz closed 9 months ago

mboullouz commented 9 months 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 9 months 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.