Simple Way to Change The Authentication Views on Laravel Fortify Package

Laravel 2 mins read

Table of Content

    Introduction

    Since Laravel, fortify is a frontend agnostic authentication backend implementation for Laravel, meaning. It's a backend authentication to let your users' login, register, logout... And all the kind of authentication implementation you want in your application but, you must notice that Laravel fortify it's just a backend scaffolding, and you need to handle your frontend by your own and set up the frontend however you like and we any frontend framework you need.

    In this quick article we learn how to install, and structure your frontend scaffolding and of course, link them with Laravel fortify.

    Requisites

    All you want to know to follow a long is a basic understanding of PHP language and Laravel framework.

    What's Laravel Fortify?

    From the documentation

    Laravel Fortify is a frontend agnostic authentication backend implementation for Laravel. Fortify registers the routes and controllers needed to implement all of Laravel's authentication features, including login, registration, password reset, email verification, and more. After installing Fortify, you may run the route:list Artisan command to see the routes that Fortify has registered.

    Since Fortify does not provide its own user interface, it is meant to be paired with your own user interface which makes requests to the routes it registers. We will discuss exactly how to make requests to these routes in the remainder of this documentation.

    Install Laravel Fortify in Existing Laravel App

    To get started, install Fortify using the Composer package manager:

    composer require laravel/fortify
    

    Next, publish Fortify's resources using the vendor:publish command:

    php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
    

    This command will publish Fortify's actions to your app/Actions directory, which will be created if it does not exist. In addition, fortify's configuration file and migrations will be published.

    Next, you should migrate your database:

    php artisan migrate
    

    After this steps, fortify will copy the controllers into your app/Http/Controllers/Auth directory and publish the views as well in resources/views/auth.

    Create Config File

    You need to create a config file, 'not necessary', but preferable.

    Create a config file under 'config/your-file.php' and called it whatever you like and add the following lines

    <?php
    
    return [
        'alternative-fortify-views'    => [
            'loginView' => 'login',
            'registerView' => 'register',
            'twoFactorChallengeView' => 'two-factor-challenge',
            'resetPasswordView' => 'reset-password',
            'verifyEmailView' => 'verify-email',
            'confirmPasswordView' => 'confirm-password',
            'requestPasswordResetLinkView' => 'forgot-password',
            'resetPasswordView' => 'reset-password',
        ],
    ];
    

    Now, we create a config file to call it wherever we want and the keys in this array is Fortify Methods to change the views and the values is Blade File Names.

    Setup Fortify Views

    We came into the important part which is changing fortify views. So, All what you need to do is to Go in app/Providers/FortifyServiceProvider or any service provider you want and under boot() method add the following lines

        foreach (config('your-config-file-name.alternative-fortify-views') as $method => $view) {
            Fortify::$method(fn () => view('path.to.auth.folder.' . $view));
        }
    

    Replace your-config-file-name with the actual name, and path.to.auth.folder with your auth folder you want to place your auth scaffolding on it.

    The meaning of these two line is. We are accessing the array from the config file and loop throw it, and it's like we are typing:

    Fortify::loginView(fn () => view('path.to.auth.folder.login'));
    

    The fn () => .... is a shorthand function came with PHP V7 and up, and if you are using PHP version below 7 consider changing it to a normal function like this.

    Fortify::$method(function () {
    	return view('path.to.auth.folder.' . $view);
    });
    

    Important Note

    You should notice how the default views looks like and follow the structure unless you made changes on your auth scaffolding such as adding a username or address etc.

    You can Browse the default auth views under resources/views/auth

    Conclusion

    In this article, we learned how to set up and add Laravel fortify auth views, and how to change them with easy and elegant way.

    If you have any questions, put them below in the comments section, and I'm happy to answer them.

    Hope this help someone around.


    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer

    I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


    Comments

    Join Our Newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    Our news letter sent once a week, every tuesday.