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:
1composer require laravel/fortify
Next, publish Fortify's resources using the vendor:publish
command:
1php 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:
1php 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
1<?php 2 3return [ 4 'alternative-fortify-views' => [ 5 'loginView' => 'login', 6 'registerView' => 'register', 7 'twoFactorChallengeView' => 'two-factor-challenge', 8 'resetPasswordView' => 'reset-password', 9 'verifyEmailView' => 'verify-email',10 'confirmPasswordView' => 'confirm-password',11 'requestPasswordResetLinkView' => 'forgot-password',12 'resetPasswordView' => 'reset-password',13 ],14];
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
1foreach (config('your-config-file-name.alternative-fortify-views') as $method => $view) {2 Fortify::$method(fn () => view('path.to.auth.folder.' . $view));3}
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:
1Fortify::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.
1Fortify::$method(function () {2 return view('path.to.auth.folder.' . $view);3});
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.