I’m proud to announce that our team has launched a new package called spatie/laravel-login-link.
In this blog post, I’d like to tell you all about it.
What is a login link, and why should you use it?
When developing an app with an admin section (or any non-public section), you’ll likely seed test users to log in. In large teams that work on many different apps, it can be cumbersome to keep track of the right credentials. Is the user account “[email protected]”, or “[email protected]”, or even “[email protected]”? Is that password “password”, “secret”, or something else? How do I log in with a user that has a different role?
This package solves that problem by offering a component to render a login link. When clicked, you will be logged in.
Beware, however, that you should never display login links in publicly reachable environments, as it will allow anyone to log in.
Rendering a login link
You can add the x-login-link component to show the login link in your login view. The @env(‘local’) will ensure that the links are only rendered in the local environment.
@env(‘local’)
<div class=“space-y-2”>
<x-login-link email=“[email protected]” label=“Login as admin”/>
<x-login-link email=“[email protected]” label=“Login as regular user”/>
</div>
@endenv
Here’s how that might look like in the browser:
The package will log in the first user in your users table by default, but that can be customised. You can add an email attribute, and the user with that mail address will be logged in.
<x-login-link email=“[email protected]” />
Alternatively, you can specify the key of the user (in most cases, this will be the id)
<x-login-link id=“123” />
You can also specify the attributes of the user that needs to be logged in.
<x-login-link :user-attributes=“[‘role’ => ‘admin’]” />
If the user that needs to be logged in does not exist, the package will use the factory of your user model to create the user and log that new user in. If you don’t want this behaviour, set automatically_create_missing_users in the local-link config file to false.
A login link will redirect you to /. That can be customised by passing a redirect-url attribute.
<x-login-link redirect-url={{ route(‘dashboard’) }} />
A note on security
Should you want to install the package, we highly recommend installing it as a dev dependency.
composer install spatie/laravel-login-link –dev
By installing the package this way, its code won’t even be present on your production server. Also, make sure that you only render the links in a local environment by wrapping it in an env check.
@env(‘local’)
<x-login-lin>
@endev
In closing
My colleague Rias was the first in our team to add a login link to a project. He got the idea from a blog post (but doesn’t remember which one 😅). Because we see ourselves using a login link in every future project, we decided to package it up. We hope that this package will also be handy for your projects.
This isn’t the first package that our team has built. On our company website, check out all our open source packages in this long list. If you want to support us, consider picking up any of our paid products.