Ad

Laravel 5.2 Add Data To Registration Without Adding To Form

- 1 answer

right now im stuck at some laravel 5.2 basics. The problem i want to solve is, that I want to add an api-key to a user upon the registration process, which works but it's not safe as it is now.

Right now I have added the API-Key to the registration form, but this would give the possibility that an attacker could change it on form send. Is there a possibility to remove that field from the form and add it to the Request before its processed by the framework? Or anything else how i can add an api key to the registered user upon insert into the database?

Thank you very much in advance for your help!

Ad

Answer

If you are using Laravel's default authentication system you can modify create method like this:

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'api_key' => code to generate your random string; // add this line
        'password' => bcrypt($data['password']),
    ]);
}
Ad
source: stackoverflow.com
Ad