Project

General

Profile

Is it valid to have multiple AuthWidget instances in one Wt application?

Added by Ali Ali 2 months ago

Hi everyone,

I’m currently using Wt::Auth::AuthWidget in my application for user authentication.
At the moment, I create a single AuthWidget instance inside my main application class and access it from other widgets (like my navigation bar or some CTA buttons) through:

auto app = dynamic_cast<MyApp*>(Wt::WApplication::instance());
auto auth = app->authWidget();

However, in some situations, I feel it might be more convenient to have separate AuthWidget instances for each section — for example, one in the navbar, one in a sidebar, and one in a modal dialog.

Before doing that, I’d like to make sure:
👉 Is it valid or safe to create multiple AuthWidget objects in the same WApplication?
Do they share the same Session and Login properly, or could that lead to inconsistent behavior or crashes?

I also noticed that sometimes I need to pass ownership of the AuthWidget (as a unique_ptr) to other Wt widgets, which makes it harder to keep a single shared instance.

What’s the recommended pattern for handling this — should I always have only one AuthWidget and connect other widgets through signals, or is it okay to create multiple ones if they all use the same Session?

Thanks in advance for any clarification or best practices!


Replies (2)

RE: Is it valid to have multiple AuthWidget instances in one Wt application? - Added by Matthias Van Ceulebroeck about 2 months ago

Hi Ali,

apologies for the delay.

Is it valid or safe to create multiple AuthWidget objects in the same WApplication?

It should not be a problem to create multiple AuthWidgets. They can use the same Login object.
I would advise to create a singular instance of a Wt::Dbo::Session from which you can retrieve the unique Login, UserDatabase, AuthService, and PasswordService.

Do note, that if you are calling processEnvironment()), there may be some iffiness with the tokens (as they are immediately replaced). So, be sure to keep that in mind (and only call that on a single instance).


I also noticed that sometimes I need to pass ownership of the AuthWidget (as a unique_ptr) to other Wt widgets, which makes it harder to keep a single shared instance.

Yes, this is simply the way Wt works. To ensure that clean-up can happen correctly, developers always pass ownership to anything in the widget tree to Wt itself.


What’s the recommended pattern for handling this --

Personally, I think having a singular instance is likely best. If you have multiple places where the widget would be placed, maybe it's better to have a single endpoint to handle this, and have buttons/text/redirects to that single page from the UI components.
Then, if the context does matter, you can pass some info via those widgets that redirect to the login page, and then attach signals to handle further logic.

As I said, it is possible to have multiple instances, which shouldn't give you too many issues, but there may be a few idiosyncrasies.

I hope this answers everything!

Best,
Matthias

    (1-2/2)