Laravel Authentication Setup
1. Install Laravel Fresh Setup
To start, install a fresh Laravel project using the following command:
composer create-project --prefer-dist laravel/laravel Blog
2. Configure Database
After installing Laravel, configure your database settings in the .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Then, run migrations to create database tables:
php artisan migrate
3. Define Routes
Update routes/web.php
to include authentication routes:
use App\Http\Controllers\AuthController;
Route::get('login', [AuthController::class, 'index']);
Route::post('post-login', [AuthController::class, 'postLogin']);
Route::get('registration', [AuthController::class, 'registration']);
Route::post('post-registration', [AuthController::class, 'postRegistration']);
Route::get('dashboard', [AuthController::class, 'dashboard']);
Route::get('logout', [AuthController::class, 'logout']);
4. Create Controller
Generate the authentication controller:
php artisan make:controller AuthController
Then, update app/Http/Controllers/AuthController.php
with the following code:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use App\Models\User;
class AuthController extends Controller
{
public function index() {
return view('login');
}
public function registration() {
return view('registration');
}
public function postLogin(Request $request) {
$request->validate([
'email' => 'required',
'password' => 'required',
]);
if (Auth::attempt($request->only('email', 'password'))) {
return redirect()->intended('dashboard');
}
return redirect('login')->with('error', 'Invalid credentials');
}
public function postRegistration(Request $request) {
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return redirect('dashboard')->with('success', 'Registration successful!');
}
public function dashboard() {
return Auth::check() ? view('dashboard') : redirect('login')->with('error', 'Access denied');
}
public function logout() {
Session::flush();
Auth::logout();
return redirect('login');
}
}
5. Create Blade Views
Create three Blade view files: login.blade.php
, registration.blade.php
, and dashboard.blade.php
.
resources/views/login.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h3 class="text-center">Login</h3>
<form action="{{ url('post-login') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
<p>Don't have an account? <a href="{{ url('registration') }}">Register</a></p>
</form>
</div>
</body>
</html>
6. Run Development Server
Start your Laravel application using:
php artisan serve
Conclusion
You have successfully set up authentication in Laravel, including login, registration, and dashboard pages. Modify the views and styles as needed to fit your project.