lonnieezell / myth-auth

One-stop Auth package for CodeIgniter 4
MIT License
633 stars 207 forks source link

Required fields for registration #203

Closed MichalPB1 closed 4 years ago

MichalPB1 commented 4 years ago

Hi, Have you thought about adding the option of registering a user by username or email? Currently both fields are required. In a situation where we do not need a username, registration is not possible because UserModel has validations as follows:

    protected $validationRules = [
        'email'         => 'required|valid_email|is_unique[users.email,id,{id}]',
        'username'      => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username,id,{id}]',
        'password_hash' => 'required',
    ];
michalsn commented 4 years ago

I'm not sure if there is a need for providing a special config for this. I would just publish AuthController and make a one-liner change in attemptRegister() method, like:

$user = new User($this->request->getPost());
$user->username = $user->email;

That should work.

Besides that, right now we kind of require the email field to be there since there is no other channel for register or reset the password.

michalsn commented 4 years ago

I advised you poorly here. It wasn't so easy as I described because of the rules in UserModel... and you even mentioned it :)

Anyway... luckily for you I prepared a PR for CodeIgniter core, and it was merged, so you can now change this validation rules in model dynamically, via $model->setValidationRule($name, $rules) - sorry, idk if there is a dev build of the user guide, so here are the details.

MichalPB1 commented 4 years ago

Hehe :) Thanks for the info

I have temporarily overwritten UserModel and changed the validation rules. The controller needed to be overwritten anyway, so it wasn't too bad. But I'll look at this new functionality

lonnieezell commented 4 years ago

Another solution is to extend the UserModel with your own App\Models\UserModel and override the settings there. Something that I typically do, anyway, so that I can add custom fields and methods to the model.