{"flag":true,"single":true,"pageTitle":"login \/ register in laravel, Auth + Redirect user to login if not authenticated || login throttling \/ account lockout.","post":{"id":8,"user_id":"1","slug":"login-register-in-laravel-authentication-onby","title":"login \/ register in laravel, Auth + Redirect user to login if not authenticated || login throttling \/ account lockout.","body":"<p><strong>With laravel we can <\/strong>make login, register pages and databases<\/p>\r\n<p><strong>1.Run command&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>composer require laravel\/ui <\/code><\/pre>\r\n<p>it will create<\/p>\r\n<p><strong>???? app\/Http\/Controllers\/Auth\/<\/strong><\/p>\r\n<ol>\r\n<li>LoginController.php<\/li>\r\n<li>RegisterController.php<\/li>\r\n<li>ForgotPasswordController.php<\/li>\r\n<li>ResetPasswordController.php<\/li>\r\n<li>VerificationController.php<\/li>\r\n<\/ol>\r\n<p>it will create <strong>ui package , views, migration, model controller<\/strong> create inside <strong>vendor\/laravel\/ui<\/strong><\/p>\r\n<p><strong>2. Run command&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan ui bootstrap --auth\r\n<\/code><\/pre>\r\n<p>bootstrap is optional if you want to use your own design<\/p>\r\n<p>it will create controllers, views and routes for login, register, forget, and migrations.<\/p>\r\n<p><strong>Controllers<\/strong>: Creates authentication-related controllers (e.g., LoginController, RegisterController) in <strong>app\/Http\/Controllers\/Auth\/.<\/strong><br><strong>Views<\/strong>: Generates Blade templates (e.g., login.blade.php, register.blade.php) in <strong>resources\/views\/auth\/.<\/strong><br><strong>Routes<\/strong>: Adds authentication routes (e.g., \/login, \/register, \/logout) to routes\/web.php.<br><strong>Migrations<\/strong>: Creates a migration file for the users table (and possibly password_resets) in d<strong>atabase\/migrations\/.<\/strong><\/p>\r\n<p><strong>3. Run migrations<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan migrate<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>AFTER THAT, Now you can <strong>Protect Routes , If user is not logged in &rarr; redirected to \/login<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::middleware(['auth'])-&gt;group(function () {\r\n\r\n    Route::get('\/dashboard', function () {\r\n        return view('dashboard');\r\n    });\r\n\r\n});\r\n<\/code><\/pre>\r\n<p>Protect controller<\/p>\r\n<pre class=\"language-markup\"><code>public function __construct()\r\n{\r\n    $this-&gt;middleware('auth');\r\n}<\/code><\/pre>\r\n<p>route prevent<\/p>\r\n<pre class=\"language-markup\"><code>Route::get('\/', function () {\r\n    return view('home');\r\n})-&gt;middleware('auth');\r\n<\/code><\/pre>\r\n<p style=\"text-align: center;\"><em><strong>Below commands 4,5 are optional, if you want to install bootsrap and compile then use these<\/strong><\/em><\/p>\r\n<p>4.Run command , below commands is to make design and install bootstrap<\/p>\r\n<pre class=\"language-markup\"><code>npm install<\/code><\/pre>\r\n<p>It<strong> installs JavaScript and CSS dependencies<\/strong> listed in your package.json file using npm (Node Package Manager)&nbsp;but they aren&rsquo;t yet compiled or usable in your views until the next step.<\/p>\r\n<p>5. Run command<\/p>\r\n<pre class=\"language-markup\"><code>npm run development<\/code><\/pre>\r\n<p>It will compile all CSS, JavaScript, etc using Laravel Mix, which is a wrapper around Webpack..&nbsp;<br>Compiles Bootstrap&rsquo;s CSS and JavaScript (from <strong>node_modules<\/strong>\/) into a single<strong> public\/css\/app.css and public\/js\/app.js.<\/strong><br>Processes any custom JavaScript or Sass you&rsquo;ve added in resources\/.<br>You can include them in your Blade templates (e.g., &lt;link href=\"{{ asset('css\/app.css') }}\" rel=\"stylesheet\"&gt;) to apply Bootstrap styling.<\/p>\r\n<p><strong>NOTE:<\/strong><\/p>\r\n<p>IF there is any error run these commands<\/p>\r\n<pre class=\"language-markup\"><code>npm install --save-dev cross-env1\r\nOR\r\nrm -rf node_modules \/\/remove node modules\r\nrm package-lock.json yar.lock\r\nnpm cache clear --force\r\nnpm install\r\n\r\n<\/code><\/pre>\r\n<p>Also run migrations<\/p>\r\n<p><strong>REDIRECT USER TO login page if not authenticated<\/strong><\/p>\r\n<p>If you want to create a custom middleware to redirect users if they are not authenticated, you can create a RedirectIfNotAuthenticated middleware in Laravel. Here are the steps to implement it:<br>1.Create a new middleware class by running the following command in your Laravel project's root directory:<\/p>\r\n<pre class=\"language-markup\"><code>php artisan make:middleware RedirectIfNotAuthenticated<\/code><\/pre>\r\n<p>2. Open the<strong> app\/Http\/Middleware\/RedirectIfNotAuthenticated.php<\/strong> file and modify the handle method to redirect the user if they are not authenticated. Here's an example implementation:<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nnamespace App\\Http\\Middleware;\r\nuse Closure;\r\nuse Illuminate\\Http\\Request;\r\nclass RedirectIfNotAuthenticated\r\n{\r\n    public function handle($request, Closure $next)\r\n    {\r\n        if (!auth()-&gt;check()) {\r\n           return redirect('\/login')-&gt;with(\"status\",\"Login First to access this page\");\r\n        }\r\n\r\n        return $next($request);\r\n    }\r\n}\r\n<\/code><\/pre>\r\n<p>3. Register the middleware in the app\/Http\/Kernel.php file. Locate the $<strong>routeMiddleware <\/strong>property and add the following line:<\/p>\r\n<pre class=\"language-markup\"><code>'notAuth' =&gt; \\App\\Http\\Middleware\\RedirectIfNotAuthenticated::class,<\/code><\/pre>\r\n<p>4. Now, you can apply the middleware to your routes or controllers using the <strong>notAuth <\/strong>middleware key. For example:<\/p>\r\n<pre class=\"language-markup\"><code>Route::controller(\\App\\Http\\Controllers\\AffliateController::class)-&gt;middleware(['notAuth'])-&gt;group(function () {\r\n    Route::get('affliate', 'index')-&gt;name('affliate');\r\n});<\/code><\/pre>\r\n<p>In this example, the <strong>notAuth <\/strong>middleware is applied to a group of routes that should only be accessible to unauthenticated users.<\/p>\r\n<p>By creating and using the RedirectIfNotAuthenticated middleware, you can easily redirect users who are not authenticated to a specific URL or route in your Laravel application.<\/p>\r\n<p><strong>==================<\/strong><br><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOGIC<\/strong><br><strong>==================<\/strong><\/p>\r\n<p><strong>app\/Http\/Controllers\/Auth\/LoginController.php<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Foundation\\Auth\\AuthenticatesUsers;\r\nclass LoginController extends Controller\r\n{\r\n    use AuthenticatesUsers; \r\n    protected $redirectTo = '\/home';\r\n}\r\n<\/code><\/pre>\r\n<p><strong>AuthenticatesUsers <\/strong>trait already contains full login logic:<\/p>\r\n<ol>\r\n<li>Validate email &amp; password<\/li>\r\n<li>Check user in database<\/li>\r\n<li>Compare hashed password<\/li>\r\n<li>Create session<\/li>\r\n<li>Redirect after login<\/li>\r\n<\/ol>\r\n<p>=======================<br><strong data-start=\"28\" data-end=\"66\">login throttling \/ account lockout<br><\/strong>=======================<br>Laravel already has this feature built-in inside: <strong>Illuminate\\Foundation\\Auth\\AuthenticatesUsers<br><\/strong>It uses another trait internally: ThrottlesLogins<\/p>\r\n<pre class=\"language-markup\"><code>class LoginController extends Controller\r\n{\r\n    use AuthenticatesUsers;\r\n    protected $redirectTo = '\/dashboard'; \/\/after successfull login\r\n    protected $maxAttempts = 2; \/\/ allow only 2 attempts\r\n    protected $decayMinutes = 60; \/\/ lock for 60 minutes\r\n}<\/code><\/pre>\r\n<p>&nbsp;Internally it use this file: vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Auth\/ThrottlesLogins.php<\/p>\r\n<p>It uses:<\/p>\r\n<ol>\r\n<li>Cache<\/li>\r\n<li>IP + email combination<\/li>\r\n<li>Rate limiter<\/li>\r\n<\/ol>\r\n<p>If You Want Custom Error Message Override this method in <strong>LoginController<\/strong>:<\/p>\r\n<pre class=\"language-markup\"><code>protected function sendLockoutResponse(\\Illuminate\\Http\\Request $request)\r\n{\r\n    $seconds = $this-&gt;limiter()-&gt;availableIn(\r\n        $this-&gt;throttleKey($request)\r\n    );\r\n\r\n    $minutes = ceil($seconds \/ 60);\r\n\r\n    throw \\Illuminate\\Validation\\ValidationException::withMessages([\r\n        'email' =&gt; \"Too many login attempts. Please try again in $minutes minute(s).\"\r\n    ]);\r\n}\r\n<\/code><\/pre>\r\n<p>&nbsp;<\/p>","category_id":"2","is_private":"0","created_at":"2023-03-14T04:16:12.000000Z","updated_at":"2026-02-18T06:29:36.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"With laravel we can make login, register pages and databases 1.Run command&nbsp; composer require laravel\/ui  it will create ???? app\/Http\/C - login \/ register in laravel, Auth + Redirect user to login if not authenticated || login throttling \/ account lockout. (Updated: February 18, 2026) - Read more about login \/ register in laravel, Auth + Redirect user to login if not authenticated || login throttling \/ account lockout. at my programming site [SITE]","categories":[]}