Blog Detail

03

Jun
A Blade Component to Quickly Login to Your Local Environment cover image

arrow_back A Blade Component to Quickly Login to Your Local Environment

When developing an app that has 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 “yourname@company.com”, or “test@company.com”, or even “admin@company.com”? Is that password “password”, or “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 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.

@env('local')
    <div class="space-y-2">
        <x-login-link email="admin@spatie.be" label="Login as admin"/>
        <x-login-link email="user@spatie.be" label="Login as regular user"/>
    </div>
@endenv

It is meant for local development, and probably shouldn’t be used in any publicly reachable environment.

Installation

You can install the package via composer.

composer require spatie/laravel-login-link

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="login-link-config"

This is the contents of the published config file:

Optionally, you can publish the views using

php artisan vendor:publish --tag="login-link-views"

Usage

To render a login link, simply add the x-login-link Blade component to your view. We highly recommend only rendering 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:

<x-login-link redirect-url="{{ route('dashboard') }}"  />

Specifying the user model to log in
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 to the first user in the table. You customize that by passing an email attribute. The user with that mail address will be logged in.

<x-login-link email="admin@example.com"  />

Alternatively, you can specify the primary 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']"  />

Specifying the login guard

By default, the package will use the default guard. You can specify another guard.

<x-login-link guard="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 behavior, set automatically_create_missing_users in the local-link config file to false.

This package has a lot more in it, If you want to dig more you can visit its complete documentation and source code on Github.

Note

If you are looking for a CRM system or eCommerce web application for your enterprise, then you are in the right place. The Web development team at Codebrisk has notable experience in the Laravel e-commerce and CRM web development field. We can provide you with custom-made e-commerce web applications with a lot of cutting-edge features and options. We will be pleased to assist you with your projects. Please feel free to contact us or you can launch a project with us.

Published at : 03-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project