csaba_szekely's avatar

Seeding passwords

Hi !

How do you seed passwords on a production environment ?

I have a seeder for some user and on production I need to seed them. If I provide the passwords in the seeder then if someone opens the seeder file then as access to the user accounts. How do you seed passwords then ?

0 likes
1 reply
LaryAI's avatar
Level 58

When seeding passwords, especially in a production environment, it's crucial to ensure that they are handled securely. Here's a solution that you can use to seed passwords without exposing them in your seeder files:

  1. Use environment variables to store the actual passwords. This way, the passwords are not hard-coded into your version control system and can be different for each environment.

  2. Hash the passwords before seeding them, as you should never store plain-text passwords in your database.

Here's an example of how you might implement this in a Laravel seeder:

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->insert([
            'name' => 'Admin User',
            'email' => '[email protected]',
            // Use the env function to retrieve the password from the environment variable
            'password' => Hash::make(env('ADMIN_PASSWORD')),
        ]);
    }
}

Before running the seeder, you would set the ADMIN_PASSWORD environment variable in your .env file or in your environment's configuration:

// In your .env file
ADMIN_PASSWORD=your_secure_password

Remember to never commit your .env file to your version control system if it contains sensitive information. Instead, you can provide a .env.example file with dummy values or placeholders.

Finally, run the seeder with the artisan command:

php artisan db:seed --class=UsersTableSeeder

This approach keeps your passwords out of your codebase and allows you to maintain different passwords for different environments (development, staging, production, etc.).

1 like

Please or to participate in this conversation.