Admin dashboard in Laravel | Sign up and Sign in

Admin dashboard in Laravel | Sign up and Sign in


Laravel 8 Authentication with Forgot Password

Step 1: Install Laravel 8 and Laravel UI

First, install a new Laravel 8 project:

composer create-project --prefer-dist laravel/laravel template_job_search

Then install Laravel UI and authentication scaffolding:

composer require laravel/ui php artisan ui vue --auth

Step 2: Configure Database and Mail

Update your .env file for database connection:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db DB_USERNAME=root DB_PASSWORD=your_database_password

Set up mail configuration for password reset:

MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD=your_email_password MAIL_ENCRYPTION=tls

Step 3: Run Migrations

Migrate the default tables (including users and password resets):

php artisan migrate

Step 4: Create Home Page

Edit resources/views/home.blade.php:

@extends('layouts.master') @section('content') <!-- [ Main Content ] start --> <!-- [ Main Content ] end --> @endsection

Step 5: Customize Register Page

Edit resources/views/auth/register.blade.php With your custom design.

Your given HTML already fits. Make sure you link assets correctly.

Step 6: Set up Routes

Edit routes/web.php:

<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Auth\RegisterController; use App\Http\Controllers\Auth\ForgotPasswordController; use App\Http\Controllers\Auth\ResetPasswordController; use App\Http\Controllers\FormController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- */ Route::get('/', function () { return view('auth.login'); }); Auth::routes(); // Home Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); // Login Route::get('/login', [LoginController::class, 'login'])->name('login'); Route::post('/login', [LoginController::class, 'authenticate']); Route::get('/logout', [LoginController::class, 'logout'])->name('logout'); // Register Route::get('/register', [RegisterController::class, 'register'])->name('register'); Route::post('/register', [RegisterController::class, 'storeUser'])->name('register.store'); // Forgot Password Route::get('forget-password', [ForgotPasswordController::class, 'getEmail'])->name('forget-password'); Route::post('forget-password', [ForgotPasswordController::class, 'postEmail']); Route::get('reset-password/{token}', [ResetPasswordController::class, 'getPassword']); Route::post('reset-password', [ResetPasswordController::class, 'updatePassword']); // Form Route::get('form/new', [FormController::class, 'index'])->name('form.new');

Step 7: Create LoginController

Create controller:

php artisan make:controller Auth/LoginController

Then define it:

<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Providers\RouteServiceProvider; class LoginController extends Controller { use AuthenticatesUsers; protected $redirectTo = RouteServiceProvider::HOME; public function __construct() { $this->middleware('guest')->except('logout'); } public function login() { return view('auth.login'); } public function authenticate(Request $request) { $credentials = $request->only('email', 'password'); $request->validate([ 'email' => 'required|email', 'password' => 'required', ]); if (Auth::attempt($credentials)) { return redirect()->intended('home'); } return redirect('login')->with('error', 'Invalid credentials.'); } public function logout() { Auth::logout(); return redirect('login'); } }

Step 8: Customize Login Page

Edit resources/views/auth/login.blade.php with your custom design.

Again, your provided HTML code is good. Fix any typos (like in your script tags) if needed.

Example:

<script src="{{URL::to('assets/js/vendor-all.min.js')}}"></script> <script src="{{URL::to('assets/plugins/bootstrap/js/bootstrap.min.js')}}"></script>

(You had a typo: a{{URL::to('ssets/plugins/bootstrap/js/bootstrap.min.js')}} → corrected.)

Step 9: Run Laravel Development Server

Finally, start your server:

php artisan serve

Visit:

http://localhost:8000

Or if you are using the public folder:

http://localhost/template_job_search/public/

Done!

You now have Login, Register, Forgot Password working properly in your Laravel 8 application.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close