What are Login Links in Laravel, and How to use them for Development?

  • Cubettech
  • Laravel Development
  • 2 years ago
What are Login Links in Laravel, and How to use them for Development?

Laravel is a straightforward cross-platform PHP framework that developers use to build web applications. However, understanding the features and benefits of the framework requires a deeper drive. 

It allows the developers to take full advantage of the extensive library of pre-programmed functions such as routing, authentication, and HTML templates. And access to the enormous library makes it simple to build web applications. 

Additionally, it offers a highly functional developmental environment for app development. Laravel uses object-relational mapping for simplified data access. As a result, it helps make robust applications and minimizes the coding process.

What is a login link, and why should you use it?

When developing an application with the admin section, there will be a seed test to log in. However, for large teams that work on multiple applications, it will become difficult to track the login process. 

It is a tedious task to keep track of credentials and logins. Also, it depends on the user account you choose, as it can vary depending on the company. Logging in a user with a different role is complex. Also, login links are an essential package that solves the problem as it offers a component to render the login link. When you click on the link, you will be logged in. 

How can you render a login link?

Log in to the local environment:

In the login view, you can add an x-login link component to show the login link. @env(‘local’ ensures that all the links are 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

The view will be different in the browser and may look something like the login page you see on the website. 

It shouldn’t be used in the publicly reachable environment if it is meant for the local environment. 

Installation process

  • It is easy to install the package via the composer. 

Composer require spatie/laravel-login-link

There is also an option for publishing the config file.

Php artisan vendor:publish –tag=”login-link-config”

Here are some of the public config rules you must know about:

using Spatie\LoginLink\Http\Controllers\LoginLinkController;

return [

    /*

     * Login links will only work in these environments. In all

     * other environments, an exemption will be thrown.

     */

    ‘allowed_environments’ => [‘local’],

    /*

     * The package will involuntarily 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’],

];

You can publish the views using the command mentioned below:

php artisan vendor: publish –tag=”login-link-views”

Rendering in local environment:

render in the local environment using:

@env(‘local’)

    <x-login-link />

@endenv

The above components let you log in when you render a link. By default, it may direct you to / , but you can customize it by specifying the route name redirect_route_name of the login link config file. 

Specifying the user model to log in:

It may use the model class provided in the user model providers.users.model, which is the key of the authorization config file. To override the changes, you can use the user_model of the login link to the class name of user models. 

Once you log in, you can customize it by pressing the email attribute. The user will be logged in.

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

After which you can specify primary key of the user,

<x-login-link id=”123″  />

Alternately, you can specify the attributes of the user <x-login-link :user-attributes=”[‘role’ => ‘admin’]”  />

How to customize the login link:

The package will display the developer login; you can customize that by passing a label attribute. 

<x-login-link label=”Click here to log in”>

Here the link will have a tailwind class that reads underline by default. You can press any CSS class if you want to customize it. The classes will overwrite the underline default. 

Use this for creating an underline link:

<x-login-link class=”underline text-red-500″>

Auto user creation:

If the user trying to log in does not exist, the package will use the factory of user model to create a new user. To avoid the situation, you can set automatically_create_missing_users in the local link config file to false. 

Usage features in other environments:

Out of the box, the login link will work in other environments. Besides, if you want to use it in the environment, set the allow environment key of the login link config files to the names of those environments. 

Be careful not to display where it can be publicly reachable. For testing, you can use the composer test. 

Conclusion

You can add login links to the package by following the above steps. Make sure you keep a tab on the security part while using the links and components in the local environment. Login links benefit the development process as they save time on a project.

Got a similar project idea?

Connect with us & let’s start the journey!

Questions about our products and services?

We're here to support you.

Staff augmentation is a flexible workforce strategy companies adopt to meet specific project needs or address skill gaps.

Begin your journey!
Need more help?