{"id":3059,"date":"2020-08-06T05:48:06","date_gmt":"2020-08-06T05:48:06","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3059"},"modified":"2026-02-10T10:45:01","modified_gmt":"2026-02-10T10:45:01","slug":"user-registration-and-login-in-laravel","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/","title":{"rendered":"User Registration And Login In Laravel"},"content":{"rendered":"<p>There are two ways to register and login user in Laravel.<\/p>\n<ol>\n<li>By using Laravel Authentication<\/li>\n<li>By making our own Login and Registration files using Laravel MVC structure.<\/li>\n<\/ol>\n<p><iframe loading=\"lazy\" title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/Vq6yI28q9p8?rel=0\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><br \/>\n<\/iframe><\/p>\n<ol>\n<li>\n<h2>By using Laravel Authentication<\/h2>\n<p><strong>Step 1:<\/strong> Create your project using this command:<br \/>\n<code>composer create-project --prefer-dist laravel\/laravel project-name<\/code><\/p>\n<p><strong>Step 2:<\/strong> Run these commands on the terminal:<br \/>\n<code><br \/>\ncomposer require laravel\/ui<br \/>\nphp artisan ui vue --auth<br \/>\nnpm install &amp;&amp; npm run dev<br \/>\n<\/code><\/p>\n<p><strong>Step 3: <\/strong>Create a database and set the database configurations inside .env file and<br \/>\ninside config &gt; database.php.<br \/>\n<strong>.env file:<\/strong><br \/>\n<code><br \/>\nDB_DATABASE=auth<br \/>\nDB_USERNAME=root<br \/>\nDB_PASSWORD=password<br \/>\n<\/code><\/p>\n<p><strong>config&gt;database.php:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/code1.png\" alt=\"code1\" \/><\/p>\n<p><strong>Step 4:<\/strong> Run the following command in terminal:<br \/>\n<code>php artisan migrate<\/code><br \/>\nYou are all set to register the user and login.<\/li>\n<li>\n<h3>By making our own Login and Registration Files using Laravel MVC Structure<\/h3>\n<p>As we know Laravel follows MVC structure, i.e. Model View Controller Structure.<\/p>\n<p><strong>VIEW:<\/strong> View contains HTML part of our application and separates our application logic from presentation logic.<\/p>\n<p><strong>CONTROLLER: <\/strong> In Controller, all our logical and business logical part is defined. The controller contains request handling logic within a single function. From view Part, our data is sent to the controller and from the controller, we send our data to Model to save it to the database.<\/p>\n<p><strong>MODEL:<\/strong> Model is a class that represents the logical structure and relationship of the underlying data table. In Laravel, each database table has a corresponding Model that allows us to interact with that table. The model gives us the way to retrieve, insert, update, and delete information from the data table.<\/p>\n<p>You need to create a Laravel Project first by using this command.<br \/>\n<code>composer create-project --prefer-dist laravel\/laravel project-name<\/code><\/p>\n<p><strong>STEP 1: CREATE VIEW PAGES FOR REGISTRATION AND LOGIN<\/strong><\/p>\n<p>Create view pages for Registration Page and Login Page.<\/p>\n<p>views&gt;registration.blade.php<br \/>\nviews&gt;login.blade.php<\/p>\n<p>We can also create a layout file in which we can write common code that can be used all over the <a href=\"https:\/\/studysection.com\/blog\/mobile-app-testing\/\">application<\/a>. We can extend the layout file code by extending the file in other view files by using directives.<br \/>\n<code>@extends('layout')<\/code><\/p>\n<p><strong>layout.blade.php:<\/strong><\/p>\n<p><code><br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Restaurant Website<br \/>\n&lt;\/title&gt;<br \/>\n&lt;style&gt;<br \/>\n.content{<br \/>\nbackground-image:linear-gradient(to right,#C9D6FF,#E2E2E2);<br \/>\nheight:600px;<br \/>\n}<br \/>\n&lt;\/style&gt;<br \/>\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css\" integrity=\"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW\/dAiS6JXm\" crossorigin=\"anonymous\"&gt;<br \/>\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.2.1.slim.min.js\" integrity=\"sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr\/rE9\/Qpg6aAZGJwFDMVNA\/GpGFF93hXpG5KkN\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;<br \/>\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.12.9\/umd\/popper.min.js\" integrity=\"sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K\/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;<br \/>\n&lt;script src=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/js\/bootstrap.min.js\" integrity=\"sha384-JZR6Spejh4U02d8jOt6vLEHfe\/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;header&gt;<br \/>\n&lt;nav class=\"navbar navbar-expand-lg navbar-light\" style=\"background:#D76D77\"&gt;<br \/>\n&lt;a class=\"navbar-brand\" href=\"\/\"&gt;RESTO&lt;\/a&gt;<br \/>\n&lt;button class=\"navbar-toggler\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarNavAltMarkup\" aria-controls=\"navbarNavAltMarkup\" aria-expanded=\"false\" aria-label=\"Toggle navigation\"&gt;<br \/>\n&lt;span class=\"navbar-toggler-icon\"&gt;&lt;\/span&gt;<br \/>\n&lt;\/button&gt;<br \/>\n&lt;div class=\"collapse navbar-collapse\" id=\"navbarNavAltMarkup\"&gt;<br \/>\n&lt;div class=\"navbar-nav\"&gt;<br \/>\n@if(Session::get('user'))<br \/>\n&lt;a class=\"nav-item nav-link\" href=\"\/list\"&gt;List&lt;\/a&gt;<br \/>\n&lt;a class=\"nav-item nav-link\" href=\"\/add\"&gt;Add&lt;\/a&gt;<br \/>\n@endif<br \/>\n&lt;&lt;\/div&gt;<br \/>\n&lt;div class=\"navbar-nav ml-auto\"&gt;<br \/>\n@if(Session::get('user'))<br \/>\n&lt;a class=\"nav-item nav-link\" href=\"#\"&gt;Welcome, {{Session::get('user')}}&lt;\/a&gt;<br \/>\n&lt;a class=\"nav-item nav-link\" href=\"\/logout\"&gt;Logout&lt;\/a&gt;<br \/>\n@else<br \/>\n&lt;a class=\"nav-item nav-link active\" href=\"\/login\"&gt;Login&lt;\/a&gt;<br \/>\n&lt;a class=\"nav-item nav-link active\" href=\"\/register\"&gt;Register&lt;\/a&gt;<br \/>\n@endif<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/nav&gt;<br \/>\n&lt;\/header&gt;<br \/>\n&lt;div class=\"content d-flex justify-content-center\"&gt;<br \/>\n@yield('content')<br \/>\n&lt;\/div&gt;<br \/>\n&lt;footer class=\"container\"&gt;&lt;\/footer&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n<\/code><\/p>\n<p><strong>registration.blade.php:<\/strong><\/p>\n<p><code><br \/>\n@extends('layout')<\/code><\/p>\n<p>@section(&#8216;content&#8217;)<br \/>\n&lt;div class=&#8221;col-sm-8&#8243;&gt;<br \/>\n&lt;h3&gt;Register User&lt;\/h3&gt;<br \/>\n@if(Session::get(&#8216;register_status&#8217;))<br \/>\n&lt;div class=&#8221;alert alert-success alert-dismissible fade show&#8221; role=&#8221;alert&#8221;&gt;<br \/>\n{{Session::get(&#8216;register_status&#8217;)}}<br \/>\n&lt;button type=&#8221;button&#8221; class=&#8221;close&#8221; data-dismiss=&#8221;alert&#8221; aria-label=&#8221;Close&#8221;&gt;<br \/>\n&lt;span aria-hidden=&#8221;true&#8221;&gt;\u00d7&lt;\/span&gt;<br \/>\n&lt;\/button&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@endif<br \/>\n&lt;form action=&#8221;registerUser&#8221; method=&#8221;post&#8221; return=&#8221;false&#8221;&gt;<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Name<br \/>\n&lt;input type=&#8221;text&#8221; name=&#8221;name&#8221; value=&#8221;{{ old(&#8216;name&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Name&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8216;name&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n@csrf<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Email<br \/>\n&lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; value=&#8221;{{ old(&#8217;email&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Email&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8217;email&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Password<br \/>\n&lt;input type=&#8221;password&#8221; name=&#8221;password&#8221; value=&#8221;{{ old(&#8216;password&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Password&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8216;password&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Confirm Password<br \/>\n&lt;input type=&#8221;password&#8221; name=&#8221;confirm_password&#8221; value=&#8221;{{ old(&#8216;confirm_password&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Confirm Password&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8216;confirm_password&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Mobile&lt;\/label&gt;<br \/>\n&lt;input type=&#8221;number&#8221; name=&#8221;mobile&#8221; value=&#8221;{{ old(&#8216;mobile&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Mobile Number&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8216;mobile&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;button type=&#8221;submit&#8221; class=&#8221;btn btn-primary&#8221;&gt;Submit&lt;\/button&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@endsection<\/p>\n<p>&nbsp;<\/p>\n<p><strong>login.blade.php:<\/strong><\/p>\n<p><code><br \/>\n@extends('layout')<\/code><\/p>\n<p>@section(&#8216;content&#8217;)<br \/>\n&lt;div class=&#8221;col-sm-8&#8243;&gt;<br \/>\n&lt;h3&gt;Login User&lt;\/h3&gt;<br \/>\n@if(Session::get(&#8216;error&#8217;))<br \/>\n&lt;div class=&#8221;alert alert-danger alert-dismissible fade show&#8221; role=&#8221;alert&#8221;&gt;<br \/>\n{{Session::get(&#8216;error&#8217;)}}<br \/>\n&lt;button type=&#8221;button&#8221; class=&#8221;close&#8221; data-dismiss=&#8221;alert&#8221; aria-label=&#8221;Close&#8221;&gt;<br \/>\n&lt;span aria-hidden=&#8221;true&#8221;&gt;\u00d7&lt;\/span&gt;<br \/>\n&lt;\/button&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@endif<br \/>\n&lt;form action=&#8221;loginUser&#8221; method=&#8221;post&#8221;&gt;<br \/>\n@csrf<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Email&lt;\/label&gt;<br \/>\n&lt;input type=&#8221;email&#8221; name=&#8221;email&#8221; value=&#8221;{{ old(&#8217;email&#8217;) }}&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Email&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8217;email&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;div class=&#8221;form-group&#8221;&gt;<br \/>\n&lt;label&gt;Password&lt;\/label&gt;<br \/>\n&lt;input type=&#8221;password&#8221; name=&#8221;password&#8221; class=&#8221;form-control&#8221; placeholder=&#8221;Enter Password&#8221; required&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@error(&#8216;password&#8217;)<br \/>\n&lt;div class=&#8221;alert alert-danger&#8221;&gt;{{ $message }}&lt;\/div&gt;<br \/>\n@enderror<br \/>\n&lt;button type=&#8221;submit&#8221; class=&#8221;btn btn-primary&#8221;&gt;Submit&lt;\/button&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/div&gt;<br \/>\n@endsection<\/p>\n<p>&nbsp;<\/p>\n<p><strong>STEP 2: CREATE ROUTES FOR VIEW FILES:<\/strong><\/p>\n<p>To access the Registration and Login view files, we need to create routes in web.php file. Open routes &gt; web.php<\/p>\n<p><code><br \/>\nRoute::view('register','register');<br \/>\nRoute::view('login','login');<br \/>\n<\/code><\/p>\n<p><strong>STEP 3: CREATE CONTROLLER:<\/strong><\/p>\n<p>We need to create a Controller to handle our form data. From View Files, When we submit our form data, it goes to Controller. We can make the controller by using the following command:<br \/>\n<code><br \/>\nPhp artisan make:controller RestoController<\/code><\/p>\n<p>You will find your controller under App &gt; http &gt; Controller &gt; RestoController<\/p>\n<p><strong>STEP 4: CREATE ROUTES FOR CONTROLLER:<\/strong><\/p>\n<p>Set routes for controller in web.php<\/p>\n<p><code>Route::post('registerUser','RestoController@registerUser');<br \/>\nRoute::post('loginUser','RestoController@login');<\/code><\/p>\n<p>Create the functions registerUser() and login() to handle the data. For Example, function registerUser() will handle registration data. In this function, we will validate the user\u2019s data and then pass that data to the data table to store it in the database using Model. So we need to create a Model for that particular data table in which we want to store our user\u2019s data.<\/p>\n<p><strong>STEP 5: CREATE MODEL:<\/strong><br \/>\nWe can create a model by using the following command:<br \/>\n<code>Php artisan make:model User<\/code><\/p>\n<p>The table name should always be in plural and the corresponding model name should be singular. Import Model in your Controller. After Validating, Send your data to the data table using Model.<\/p>\n<h3>REGISTER:<\/h3>\n<ul>\n<li>You need to save your password in encrypted form. We can not save passwords directly into data tables as submitted by Users.<br \/>\n<code>$encrypted_password = crypt::encrypt($data['password']);<\/code><\/li>\n<li>If User is registered successfully, Set a flash session with a message that User has been registered successfully and redirect to the same page. But if there is an error, then show that error message on view Page. We use flash sessions for this because they get expired once the page is refreshed.<br \/>\n<code><br \/>\n$req-&gt;session()-&gt;flash('register_status','User has been registered successfully');<br \/>\nreturn redirect('\/register');<\/code><\/li>\n<li>The one important thing we need to check is that the email id using which once a user has been registered should never be able to register again with the same email Id.<br \/>\n<code><code>function registerUser(Request $req){<br \/>\n$validateData = $req-&gt;validate([<br \/>\n'name' =&gt; 'required|regex:\/^[a-z A-Z]+$\/u',<br \/>\n'email' =&gt; 'required|email',<br \/>\n'password' =&gt; 'required|min:6|max:12',<br \/>\n'confirm_password' =&gt; 'required|same:password',<br \/>\n'mobile' =&gt; 'numeric|required|digits:10'<br \/>\n]);<\/code><\/code>$result = DB::table(&#8216;users&#8217;)<br \/>\n-&gt;where(&#8217;email&#8217;,$req-&gt;input(&#8217;email&#8217;))<br \/>\n-&gt;get();<code><code><\/code><\/code>$res = json_decode($result,true);<br \/>\nprint_r($res);<code><code><\/code><\/code>if(sizeof($res)==0){<br \/>\n$data = $req-&gt;input();<br \/>\n$user = new User;<br \/>\n$user-&gt;name = $data[&#8216;name&#8217;];<br \/>\n$user-&gt;email = $data[&#8217;email&#8217;];<br \/>\n$encrypted_password = crypt::encrypt($data[&#8216;password&#8217;]);<br \/>\n$user-&gt;password = $encrypted_password;<br \/>\n$user-&gt;mobile = $data[&#8216;mobile&#8217;];<br \/>\n$user-&gt;save();<br \/>\n$req-&gt;session()-&gt;flash(&#8216;register_status&#8217;,&#8217;User has been registered successfully&#8217;);<br \/>\nreturn redirect(&#8216;\/register&#8217;);<br \/>\n}<br \/>\nelse{<br \/>\n$req-&gt;session()-&gt;flash(&#8216;register_status&#8217;,&#8217;This Email already exists.&#8217;);<br \/>\nreturn redirect(&#8216;\/register&#8217;);<br \/>\n}<br \/>\n}<\/li>\n<\/ul>\n<h3>LOGIN:<\/h3>\n<ul>\n<li>After done with the Registration, We can log in. Login will also follow the same process.<\/li>\n<li>If the user logins with the email id which has been registered, he will be logged in successfully and be able to access the Home route and some other routes.<\/li>\n<li>While login, we will check whether this email id is existing in our data table or not, If not then we will tell the user to register first using flash sessions. And if email Id exists, we need to match the password. But how will we match the password?<br \/>\nBecause the user while login is giving us a simple password. But the one which we saved during registration is an encrypted password. So how will we match it?<\/li>\n<li>We can match it. We need to get the encrypted password of the corresponding email id from the data table and need to decrypt it using the decrypt function of crypt class. After decrypting, We can match both the passwords. If password matches, User can be logged in otherwise show Password Mismatch Error.<code><br \/>\nfunction login(Request $req){<br \/>\n$validatedData = $req-&gt;validate([<br \/>\n'email' =&gt; 'required|email',<br \/>\n'password' =&gt; 'required'<br \/>\n]);<\/code>$result = DB::table(&#8216;users&#8217;)<br \/>\n-&gt;where(&#8217;email&#8217;,$req-&gt;input(&#8217;email&#8217;))<br \/>\n-&gt;get();$res = json_decode($result,true);<br \/>\nprint_r($res);if(sizeof($res)==0){<br \/>\n$req-&gt;session()-&gt;flash(&#8216;error&#8217;,&#8217;Email Id does not exist. Please register yourself first&#8217;);<br \/>\necho &#8220;Email Id Does not Exist.&#8221;;<br \/>\nreturn redirect(&#8216;login&#8217;);<br \/>\n}<br \/>\nelse{<br \/>\necho &#8220;Hello&#8221;;<br \/>\n$encrypted_password = $result[0]-&gt;password;<br \/>\n$decrypted_password = crypt::decrypt($encrypted_password);<br \/>\nif($decrypted_password==$req-&gt;input(&#8216;password&#8217;)){<br \/>\necho &#8220;You are logged in Successfully&#8221;;<br \/>\n$req-&gt;session()-&gt;put(&#8216;user&#8217;,$result[0]-&gt;name);<br \/>\nreturn redirect(&#8216;\/&#8217;);<br \/>\n}<br \/>\nelse{<br \/>\n$req-&gt;session()-&gt;flash(&#8216;error&#8217;,&#8217;Password Incorrect!!!&#8217;);<br \/>\necho &#8220;Email Id Does not Exist.&#8221;;<br \/>\nreturn redirect(&#8216;login&#8217;);<br \/>\n}<br \/>\n}<br \/>\n}<\/li>\n<\/ul>\n<p><strong>NOTE:<\/strong><br \/>\nNow one more important thing to take care of authenticity is that User should not be able to access routes directly from the browser without being logged in.<br \/>\nFor that, we can use Middleware.<\/p>\n<p><strong>STEP 6: MIDDLEWARE<\/strong><\/p>\n<p><code>php artisan make:middleware customAuth<\/code><\/p>\n<p>App &gt; http &gt; middleware &gt; customAuth<\/p>\n<p>In this Middleware, We can set some conditions that if the session is not set which means if the user is not logged in, he should not be able to access the home route. If he tries to access it, he should be redirected to the login page.<\/p>\n<p><code><br \/>\n&lt;?php<\/code><\/p>\n<p>namespace App\\Http\\Middleware;<br \/>\nuse Closure;<br \/>\nuse Session;<\/p>\n<p>class customAuth<br \/>\n{<br \/>\n\/**<br \/>\n* Handle an incoming request.<br \/>\n*<br \/>\n* @param \\Illuminate\\Http\\Request $request<br \/>\n* @param \\Closure $next<br \/>\n* @return mixed<br \/>\n*\/<br \/>\npublic function handle($request, Closure $next)<br \/>\n{<br \/>\n$path = $request-&gt;path();<br \/>\nif($path==&#8221;login&#8221; &amp;&amp; Session::get(&#8216;user&#8217;)){<br \/>\nreturn redirect(&#8216;\/&#8217;);<br \/>\n}<br \/>\nelse if(($path!=&#8221;login&#8221; &amp;&amp; !Session::get(&#8216;user&#8217;)) &amp;&amp; ($path!=&#8221;register&#8221; &amp;&amp; !Session::get(&#8216;user&#8217;))){<br \/>\nreturn redirect(&#8216;\/login&#8217;);<br \/>\n}<br \/>\nreturn $next($request);<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Register Your Middleware:<\/strong><\/p>\n<p>App &gt; http &gt; kernel.php<\/p>\n<p>In this kernel.php, We can add our middleware in any middleware(i.e. Global, grouped, or Individual) and can name it. Now we will use that middleware name that we have set in Kernel.php on routes. I have added customAuth middleware as customAuth in middlewareGroups.<\/p>\n<p><code><br \/>\nprotected $middlewareGroups = [<br \/>\n'web' =&gt; [<br \/>\n\\App\\Http\\Middleware\\EncryptCookies::class,<br \/>\n\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,<br \/>\n\\Illuminate\\Session\\Middleware\\StartSession::class,<br \/>\n\/\/ \\Illuminate\\Session\\Middleware\\AuthenticateSession::class,<br \/>\n\\Illuminate\\View\\Middleware\\ShareErrorsFromSession::class,<br \/>\n\\App\\Http\\Middleware\\VerifyCsrfToken::class,<br \/>\n\\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,<br \/>\n],<\/code><\/p>\n<p>&#8216;api&#8217; =&gt; [<br \/>\n&#8216;throttle:60,1&#8217;,<br \/>\n\\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,<br \/>\n],<\/p>\n<p>&#8216;customAuth&#8217; =&gt; [<br \/>\n\\App\\Http\\Middleware\\customAuth::class<br \/>\n]\n];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Apply Middleware:<\/strong><br \/>\nApply the middleware on the routes you want to apply in web.php file.<\/p>\n<p>Web.php:<br \/>\n<code><br \/>\nRoute::group(['middleware'=&gt;'customAuth'],function(){<br \/>\nRoute::get('\/list','RestoController@list');<br \/>\nRoute::view('\/add','add');<br \/>\nRoute::post('addResto','RestoController@addResto');<br \/>\nRoute::view('register','register');<br \/>\nRoute::view('login','login');<br \/>\nRoute::get('logout','RestoController@logout');<br \/>\n});<\/code><\/p>\n<p>&nbsp;<\/li>\n<\/ol>\n<p>This will restrict unauthorized users to access the home page, list page, etc. Only authenticated users will be able to access those pages. And if once the user logged in, he would not be able to access the login and register page.<\/p>\n<h3>SCREENSHOTS:<\/h3>\n<p>Once the user is logged in, he will be able to access all the routes like home, add, list, etc. But if the user is not logged in, he would not be able to access add and list routes. He will be redirected to the login page as he needs to login first.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/resto.png\" alt=\"resto\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/screens.png\" alt=\"screens\" \/><\/p>\n<p><small><em>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/laravel-developer-advanced-diploma\">PHP certification exams<\/a> are offered by StudySection along with other programming certification exams. <\/em><\/small><\/p>\n<p><script>\njQuery(\"#top-view-post\").text('Attempt Laravel Test Now');\njQuery(\"#top-view-post\").attr(\"href\", \"https:\/\/www.studysection.com\/laravel-5.x-foundation\");\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two ways to register and login user in Laravel. By using Laravel Authentication By making our own Login<\/p>\n","protected":false},"author":1,"featured_media":3069,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[65,511],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - User Registration And Login In Laravel<\/title>\n<meta name=\"description\" content=\"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - User Registration And Login In Laravel\" \/>\n<meta property=\"og:description\" content=\"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-06T05:48:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-10T10:45:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/laravel.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"User Registration And Login In Laravel\",\"datePublished\":\"2020-08-06T05:48:06+00:00\",\"dateModified\":\"2026-02-10T10:45:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\"},\"wordCount\":1896,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"laravel\",\"Registration\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\",\"url\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\",\"name\":\"StudySection Blog - User Registration And Login In Laravel\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-08-06T05:48:06+00:00\",\"dateModified\":\"2026-02-10T10:45:01+00:00\",\"description\":\"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"User Registration And Login In Laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - User Registration And Login In Laravel","description":"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - User Registration And Login In Laravel","og_description":"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.","og_url":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-08-06T05:48:06+00:00","article_modified_time":"2026-02-10T10:45:01+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/laravel.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"User Registration And Login In Laravel","datePublished":"2020-08-06T05:48:06+00:00","dateModified":"2026-02-10T10:45:01+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/"},"wordCount":1896,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["laravel","Registration"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/","url":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/","name":"StudySection Blog - User Registration And Login In Laravel","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-08-06T05:48:06+00:00","dateModified":"2026-02-10T10:45:01+00:00","description":"There are two ways to register and login user in Laravel..By using Laravel Authentication or by making our own Login and Registration files using MVC.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/user-registration-and-login-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"User Registration And Login In Laravel"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":29133,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3059"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3059"}],"version-history":[{"count":16,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3059\/revisions"}],"predecessor-version":[{"id":8554,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3059\/revisions\/8554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3069"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}