atk4 / login

Add-on implementing User Login, Registration, Management and Password
https://agiletoolkit.org
MIT License
26 stars 22 forks source link

Cannot initialize Auth when DB field names do not match example User #36

Closed TheNetworkIsDown closed 2 years ago

TheNetworkIsDown commented 4 years ago

It appears that if you setup your DB with field names different from the example User model, that is, initialize authentication like follows:

$app->add(new \atk4\login\Auth([
  'fieldLogin'=> 'user_name',
  'fieldPassword'=>'user_password',
  ]))
  ->setModel(new \atk4\login\Model\User($app->db));

Then it will fail in \atk4\login\Feature\setupUserModel() where field names are hardcoded to "name", "email', etc.

DarkSide666 commented 4 years ago

Well, you can pass custom login and password field names in setModel() method too as 2nd and 3rd parameters, but I guess that will not help anyway because model will already be initialized.

What you surely can do is extend that trait (or Auth) and use fieldnames which you want there. Also if you use your own custom User model, then you can call setupUserModel() at some point later or call it from your custom Auth or don't call it at all and include all you need in your custom User model class.

Anyway, if you could come up with better solution for this issue, then please create PR and I will review and if good, then surely merge in. Thanks!

TheNetworkIsDown commented 4 years ago

At this point I haven't even figured out how to login even if I align my database to what the User model suggests.

How do I create the first account?

Also the login screen does not look the same as in https://github.com/atk4/login/blob/develop/README.md

Here, it looks like this:

image

TheNetworkIsDown commented 4 years ago

The hardcoded field names in the model can't be the bug. The model represents the database, so they need to be manually defined at some point. The problems arise when the different traits are used, which contain the same hardcoded references.

Usually you would define a model and additionally describe the relationships therein. That is now accomplished by calling a function residing inside the traits. But the traits in this case do more than just that. They set up the entire login system.

It's logical that if you change the model you'll also need to update the relationships. However in this case you'd need to know of all those helper functions here and there that have been introduced.

These models actually need configuration.

DarkSide666 commented 4 years ago

Idea here with these traits was that they should do all the magic necessary to make plain, any user data model usable as data model for Auth. Maybe you have your own, already existing user data model and now want to use atk4/login. So you should be able to just add trait and run trait method and your user data model should be set up for usage with Auth.

bedengler commented 4 years ago

I'm struggling with the same problem. @DarkSide666 could you elaborate more on how to implement my own database table & fieldnames for user credentials? My initialisation looks like:

$this->auth = $this->add(new \atk4\login\Auth([
        'fieldLogin' => 'user_Username',
        'fieldPassword' => 'user_Password'
]));

$m = new Betools\_General\Models\BetoolsLogin();
$this->auth->setModel($m);

And my model like:

namespace Betools\_General\Models;

use atk4\data\Model;
use atk4\data\ValidationException;
use atk4\login\Field\Password;

// Features of User model
use atk4\login\Feature\PasswordManagement;
use atk4\login\Feature\UniqueFieldValue;

class BetoolsLogin extends \atk4\data\Model {
  use PasswordManagement;
  use UniqueFieldValue;

  public $table = "betools_users";
  public $id_field = "user_ID";

  function init(): void {
    parent::init();

    $this->addField('user_Username');
    $this->addField('user_Password', [new \atk4\login\Field\Password]);

   $this->initPasswordManagement();
  }
}

I have a user in my database. As soon as I try to login, I'm getting the error:

atk4\core\Exception : Element is not found in collection Exception Parameters collection:"fields" name:"user_Username" this:Object Betools_General\Models\BetoolsLogin

Help needed please!

bedengler commented 4 years ago

I got it working now by changing the auth initialization to this:

$this->auth = $this->add(new \atk4\login\Auth([
  'fieldLogin' => 'user_Username',
  'fieldPassword' => 'user_Password',
  'form' => \Betools\_General\Forms\BetoolsLoginForm::class
]));

$m = new Betools\_General\Models\BetoolsLogin($this->db);
$this->auth->setModel($m, "user_Username", "user_Password");
DarkSide666 commented 4 years ago

Possible bug - looks like you have to use setModel() 2nd and 3rd parameter to configure field names even if you already have set fieldLogin and fieldPassword properties. Solution - if fieldLogin and fieldPassword properties are set (not default), then should use them as default values for username and password field names in setModel().

mvorisek commented 2 years ago

see tests/GenericTestCase.php file in https://github.com/atk4/login/pull/77/files

copy these builder model methods and in getClassByStandardModelClass you can tweak everything you want

closing as this is not a bug and no better solution exists without model registry