Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lloyd_mensah's avatar

How would you login in admin user with credentials in env file?

Hi, I'm relatively new to laravel. I'm trying to log in a user without any records in the users table. The plan is to have the credentials for the admin in the env file. THE QUESTION: I know how to do the comparison, i know how to redirect the user to the next page after login. What session variables should be created before redirecting the user to the next page since the attempt method and $request->session()->regenerate() won't work? TLDR; How would you login in admin user with credentials in env file?

0 likes
4 replies
PeterF's avatar

I have an admin user created as part of my migration files, that way I always have a known admin whenever I redo the database in dev or test. You can put environment checks around the creation in your migration, so you have different ones for different environments. And then of course, just do it manually in production.

lloyd_mensah's avatar

@martinbean my boss doesn't want it that way. I'm actually thinking of adding an env variable called MODE and if env('MODE') == "demo" I'll ignore the auth middleware and redirect from the login function to the dashboard. Haven't tried it but that will be my next attempt. TLDR; User should be able to login as admin. But the credentials shouldn't be in the database

martinbean's avatar

@lloyd_mensah But Laravel’s authentication guards and middleware works with users. You can’t “log in” if you don’t have a user.

So, seed your database with a user. Give them a role if you have to.

You’re the developer, not your boss. If your boss knew what they were doing then they’d be doing it themselves and wouldn’t have hired you.

1 like

Please or to participate in this conversation.