Sphinx Login invoked 2 Times

SphinxWebLogin is invoked 2 times before the user is logged in.
After the first time log in with a valid mail and password, SphinxWebLogin UserLoginArgs:=nil. SphinxWebLogin will automatically start a second time. After re-entering email address and password, UserLoginArgs has vaild user data.
HandleOAuth is set to false.
In the HTML debugger of the Browser I don't get an error.

I tested the whole thing with the SphinxWebLogin parameters both in the object inspector and directly in the source code. Neither resulted in any change.

Source of Main Program:

program pm;
{$R *.dres}
uses
  Vcl.Forms,
  WEBLib.Forms,
  proMan in 'proMan.pas' {StartForm: TWebForm} {*.html},
  proman_user_profile in 'proman_user_profile.pas' {UserProfileForm: TWebForm} {*.html},
  proman_orders in 'proman_orders.pas' {OrdersForm: TWebForm} {*.html},
  proman_datenmodul in 'proman_datenmodul.pas' {dmProMan: TWebDataModule},
  uFrame in 'uFrame.pas' {MenuFrame: TFrame};
{$R *.res}
begin
  Application.HandleOAuth := false;
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TdmProMan, dmProMan);
  Application.CreateForm(TStartForm, StartForm);
  Application.Run;
end.

And here is the source code with the "SphinxWebLogin".
I've placed the SphinxWebLogin to a datamodule. This data module is created as the first unit in the main program.
ShowMessage user LoggedIn will only be displayed after the 2nd call to SphinxWebLogin.

unit proman_datenmodul;

interface

uses
  System.SysUtils, System.Classes, JS,
  Web,
  WEBLib.Dialogs,
  WEBLib.Modules, Sphinx.WebLogin;

type
  TdmProMan = class(TWebDataModule)
    SphinxWebLogin: TSphinxWebLogin;
    procedure WebDataModuleCreate(Sender: TObject);
    procedure SphinxWebLoginUserLoggedIn(Args: TUserLoggedInArgs);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    UserArgs: TUserLoggedInArgs;
  end;

var
  dmProMan: TdmProMan;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TdmProMan.SphinxWebLoginUserLoggedIn(Args: TUserLoggedInArgs);
begin
  ShowMessage('User Logged in');
  userArgs:=Args;
end;

procedure TdmProMan.WebDataModuleCreate(Sender: TObject);
begin
  userArgs:=nil;
  with SphinxWebLogin do
    begin
      Authority:='http://178.254.40.246:2001/app/sphinx';
      ClientId:='web';
      RedirectUri:='http://178.254.40.246:2001/app/pm/';
      scope:='openid email';
      onUserLoggedIn:=SphinxWebLoginUserLoggedIn;
      Login;
    end;
end;

end. 

I've tried everything, but I just can't get anywhere at this point.
I'm grateful for any help.

Be aware that many things in Web Core apps are asynchronous, including creation of forms and data modules.

The way your app is starting, it will create both TdmProMan and TStartForm - as they are async.

I'd suggest that you only create the data module. If the login is processed correct, then you can start your application. I other words, try to launch the start form from the OnLogin event, which is the time when the user is finally logged in.

Hello Wagner,
Thanks for your quick reply.
I have changed the program accordingly and start the main form from the SphinxWebUserUserLoggedIn Procedure. This works great if the user has been logged in before. But if I have deleted the browser history and then call up my program, I have to log in again twice in a row. Only after the 2nd login will the SphinxWebUserLoggedIn procedure be executed.

I read in another post that I should call the SphinxWebLogin.login twice in a row in the source code to fix this problem. Unfortunately, this does not lead to a better result.

Greetings
Andreas

Can you please clean up your app so that we can compile and reproduce the issue at our side, and then send it to us?

Here I send you an excerpt from the project. The login and the home page. I hope this is how the problem can be found.
proman.zip (356.6 KB)

On the server side, I have set up a test database.
Thank you in advance for your help

The project is extremely big and complex for a support ticket. Server depends on 3rd party components (IndySecureMailClient, UniDAC units), the client is extremely big, full of files. I cannot easily compile neither of server nor client..

Can you please reduce it to a minimal focused on just reproducing the issue? That's also an exercise about understanding how things works and isolation possible influence on Sphinx from other code.

Nevertheless, I see your code calls Login twice. Why? I cannot run it because of the above, but you said login is called twice and you are calling the method twice, isn't it the obvious cause?

procedure TdmProMan.WebDataModuleCreate(Sender: TObject);
begin
  userArgs:=nil;
  if (not SphinxWebLogin.IsLoggedIn) then
    with SphinxWebLogin do
      begin
      {
        Authority:='http://178.254.40.246:2001/app/sphinx';
        ClientId:='web';
        RedirectUri:='http://178.254.40.246:2001/app/pm/';
        scope:='openid email';
        onUserLoggedIn:=SphinxWebLoginUserLoggedIn;
        Login;
      }
        Login;
        login;
      end;
   if sphinxWebLogin.IsLoggedIn then
     Application.CreateForm(TStartForm, StartForm);
end;

Also, why are you creating the form in OnCreate, if you also creating it in OnUserLoggedIn?