Jump to content
Ian Branch

Login Form - 3 tries.

Recommended Posts

Hi Guys,

I am trying to figure out how I can give the User 3 tries at logging in with the following code before exiting the Login form and throwing him/her out.

The following is the basic code for the Login Form;

class function TLogInForm.Execute: Boolean;
begin
  //
  with TLogInForm.Create(nil) do
    try
      Result := ShowModal = mrOk;
    finally
      Free;
    end;
  //
end;

procedure TLogInForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TLogInForm.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TLogInForm.btnOKClick(Sender: TObject);
begin
  //
  if DoLogin then
    ModalResult := mrOk
  else
    ModalResult := mrCancel;
  //
end;

function TLogInForm.DoLogin: Boolean;
begin
  //
  Result := False;
  //
  // Set The Engine, Session & Database parameters, if required.
  //
  //
  //
  // 'Condition' User ID & Password.
  sUserID := UpperCase(Trim(edtUserID.Text));
  sPassword := Trim(edtPassword.Text);
  //
  with tblUsers do
  begin
    Open;
    // Test for User ID is in DB.
    if not FindKey([sUserID]) then
    begin
      MessageDlg('User ID not found!', mtInformation, [mbOK], 0);
      Exit;
    end;
    //
    if Trim(FieldByName('UsrPasswrd').AsString) <> sPassword then
    begin
      MessageDlg('User''s password is invalid!', mtError, [mbOK], 0);
      Exit;
    end;
    //
  end;
  //
  Result := True;
  //
end;

Any assistance on how to do this is greatly appreciated.

Regards & TIA,

Ian

Share this post


Link to post
if DoLogin then 
  ModalResult := mrOk 
else 
  Inc(failcnt);
if failcnt = X then
  ModalResult := mrCancel;

eventually.. maybe you should set up the connection to the db somewhere else..

and you don't need to free the form if action is caFree or vice versa.

 

 

Share this post


Link to post

Hi Attila,

Thank you your suggestions.

I have removed the Form OnClose event.

The DoLogin is returning the correct thing if the log in is invalid and failcount gets incremented, however the routine then drops out and it displays the Main Form rather than looping back to retry the user ID & password.

Ian

Share this post


Link to post
17 minutes ago, Ian Branch said:

The DoLogin is returning the correct thing if the log in is invalid and failcount gets incremented, however the routine then drops out and it displays the Main Form rather than looping back to retry the user ID & password.

Double check to make sure that you don't have a ModalResult assigned to btnOK itself.  You should be able to click on btnOK and call DoLogin() as many times as you want without closing the Form until the Form's ModalResult is assigned when the Fail counter reaches the desired limit.

Share this post


Link to post

Hi Remy,

Good call.  Exactly as you surmised.

Made the OK button mrNone and all good now.

Thank you for the assist and thanks again to Attila.

 

Regards,

Ian

Share this post


Link to post

Hi Attila,

The login form is called from and returns to the project .dpr file.

begin
  //
  if TLogInForm.Execute then
  begin
    Application.Initialize;
    Application.CreateForm(TMainForm, MainForm);
    Application.Run;
  end
  else
  begin
    Application.MessageBox('You are not authorized to use the application.', 'Password Protected Delphi application');
  end;

end.

Regards,

Ian

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×