When developing an app that has an admin section (or any non-public section), you'll likely seed test users to login. 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", or "secret", or something is else? How do I login with a user that has a different role?
This package solves that problem by offering a component that will render a login link. When clicked, that link will log you in.
In your login view, you can add the x-login-link component to show the login link. The @env('local') will make sure that the links are only rendered in the local environment.
@endenv">
@env('local')
<divclass="space-y-2">
<x-login-linkemail="[email protected]"label="Login as admin"/>
<x-login-linkemail="[email protected]"label="Login as regular user"/>
div>
@endenv
Here's what that might look like in the browser:
It is meant for local development, and probably shouldn't be used in any publicly reachable environment.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
This is the contents of the published config file:
useSpatie\LoginLink\Http\Controllers\LoginLinkController;
return [
/* * Login links will only work in these environments. In all * other environments, an exception will be thrown. */'allowed_environments' => ['local'],
/* * The package will automatically create a user model when trying * to log in a user that doesn't exist. */'automatically_create_missing_users' => true,
/* * The user model that should be logged in. If this is set to `null` * we'll take a look at the model used for the `users` * provider in config/auth.php */'user_model' => null,
/* * After a login link is clicked, we'll redirect the user to this route. * If it is set to `null` , we'll redirect to `/`. */'redirect_route_name' => null,
/* * The package will register a route that points to this controller. To have fine * grained control over what happens when a login link is clicked, you can * override this class. */'login_link_controller' => LoginLinkController::class,
/* * This middleware will be applied on the route * that logs in a user via a link. */'middleware' => ['web'],
];
To render a login link, simply add the x-login-link Blade component to your view. We highly recommend to only render it in the local environment.
@env('local')
<x-login-link />
@endenv
This component will render a link that, when clicked, will log you in. By default, it will redirect you to /, but you can customize that by specifying a route name in the redirect_route_name of the login-link config file.
You can also specify the redirect URL on the component itself:
By default, it will use the user model class that is specified in the providers.users.model key of the auth config file. To override this, you can set the user_model of the login-link config file to the class name of your user model.
The package will log in the first user in the table. You customize that by passing an email attribute. The user with that mail address will be logged in.
By default, the package will display "Developer login" as the text of the login link. You can customize that by passing a label attribute.
">
<x-login-linklabel="Click here to log in">
A login link will have the Tailwind class underline by default. To customize that, you can pass any css class that you want to the class property. These classes will override the underline default.
Here's how you can create a red, underlined link (when using Tailwind CSS).
">
<x-login-linkclass="underline text-red-500">
Specifying the login guard
By default, the package will use the default guard. You can specify another guard.
">
<x-login-linkguard="admin">
Automatic user creation
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.
Usage with Vue / React / ...
The package doesn't come with any JS component out of the box. When you use a JS front end framework to render your views, you can still make use of the package.
You should send a POST request to /laravel-login-link-login. If you don't give it any payload, then it will log in the first user in your users table. If there is no user, it will be created.
Optionally, you can post any of these payload fields. The functionality of these payloads fields match those of the attributes that you can pass to x-login-link component.
email: attempt to log in the user with the given email address
key: attempt to log in the user with the given key (in most cases the id of the users)
redirect_url: to which URL should we redirect after logging in
user_attributes: an array containing the attributes that the user that will be logged in needs to have.
Since this is a POST request, make sure to pass a CSRF token as well.
Usage in other environments
Out of the box, the login link will only work in a local environment. If you want to use it other environments, set the allowed_environments key of the login-link config file to the names of those environments.
Beware however, that you should never display login links in any environment that is publicly reachable, as it will allow anyone to log in.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR:
@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot merge will merge this PR after your CI passes on it
@dependabot squash and merge will squash and merge this PR after your CI passes on it
@dependabot cancel merge will cancel a previously requested merge and block automerging
@dependabot reopen will reopen this PR if it is closed
@dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
This allows the user to override the logic how a user is created. It vendors a default action that does exactly the same as the package used to do before.
I am using this package with Jetstream and found myself wanting to change the logic so that teams are created alongside, this will enable users to do so while keeping it completely backwards compatible.
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR:
@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot merge will merge this PR after your CI passes on it
@dependabot squash and merge will squash and merge this PR after your CI passes on it
@dependabot cancel merge will cancel a previously requested merge and block automerging
@dependabot reopen will reopen this PR if it is closed
@dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Adding a support where user can redirect to the previously requested link after logging in, so that users don't miss the track and they don't have to travel through all the routes again.
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR:
@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot merge will merge this PR after your CI passes on it
@dependabot squash and merge will squash and merge this PR after your CI passes on it
@dependabot cancel merge will cancel a previously requested merge and block automerging
@dependabot reopen will reopen this PR if it is closed
@dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)