Laravel Sign in and Sign up
In Laravel Sign in and Sign up tutorial, we will show you how to register a user, log in, and log out in Laravel. Laravel 12 requires PHP 8.2 or higher. Laravel PHP Signup and Login System Tutorial with source code
Step 1: Create a New Laravel Project
composer create-project laravel/laravel userauth
cd userauth
Step 2: Configure Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=userauthdb
DB_USERNAME=root
DB_PASSWORD=
Step 3: Migrate Default User Table
Laravel will include a users table migration.
php artisan migrate
Step 4: Create Auth Controller
php artisan make:controller AuthController
Step 5: Controller Logic
app/
└── Http/
└── Controllers/
├── AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
// Show registration form
public function showRegister()
{
return view('register');
}
// Handle registration
public function register(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
// Create user
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->save();
return redirect('/login')->with('success', 'Registration successful! Please login.');
}
// Show login form
public function showLogin()
{
return view('login');
}
// Handle login
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect('/dashboard');
}
return back()->withErrors([
'email' => 'Invalid credentials',
])->onlyInput('email');
}
// Dashboard page (after login)
public function dashboard()
{
return view('dashboard');
}
// Logout
public function logout(Request $request)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/login');
}
}
Step 6: Define Routes
routes/
└── web.php
use App\Http\Controllers\AuthController;
Route::get('/', function () {
return redirect('/login');
});
Route::get('/register', [AuthController::class, 'showRegister']);
Route::post('/register', [AuthController::class, 'register']);
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/dashboard', [AuthController::class, 'dashboard'])->middleware('auth');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
Create Views
Make sure you have this folder and file:
resources/
└── views/
├── app.blade.php
├── register.blade.php
└── login.blade.php
└── dashboard.blade.php
Step 7: Create a common layout
📄 resources/views/app.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'User Auth')</title>
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background: linear-gradient(135deg, #4e73df, #1cc88a);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.auth-card {
background: white;
border-radius: 15px;
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
padding: 2rem;
width: 100%;
max-width: 450px;
}
h2 {
color: #4e73df;
font-weight: 600;
text-align: center;
margin-bottom: 1.5rem;
}
.form-control:focus {
box-shadow: none;
border-color: #4e73df;
}
.btn-primary {
background-color: #4e73df;
border: none;
}
.btn-primary:hover {
background-color: #2e59d9;
}
a {
text-decoration: none;
color: #1cc88a;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="auth-card">
@yield('content')
</div>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 7: Create a Registration Page
📄 resources/views/register.blade.php
@extends('app')
@section('title', 'Register')
@section('content')
<h2>Create Account</h2>
@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if($errors->any())
<div class="alert alert-danger">
<ul class="mb-0">
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="/register" method="POST">
@csrf
<div class="mb-3">
<label class="form-label">Full Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter your name" required>
</div>
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email" class="form-control" name="email" placeholder="Enter your email" required>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" class="form-control" name="password" placeholder="Enter password" required>
</div>
<div class="mb-3">
<label class="form-label">Confirm Password</label>
<input type="password" class="form-control" name="password_confirmation" placeholder="Confirm password" required>
</div>
<button type="submit" class="btn btn-primary w-100">Register</button>
<p class="text-center mt-3 mb-0">
Already have an account? <a href="/login">Login</a>
</p>
</form>
@endsection
Step 8: Create a Login Page
📄 resources/views/login.blade.php
@extends('app')
@section('title', 'Login')
@section('content')
<h2>Welcome Back</h2>
@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if($errors->any())
<div class="alert alert-danger">{{ $errors->first() }}</div>
@endif
<form action="/login" method="POST">
@csrf
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email" class="form-control" name="email" placeholder="Enter your email" required>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" class="form-control" name="password" placeholder="Enter password" required>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
<p class="text-center mt-3 mb-0">
Don’t have an account? <a href="/register">Register</a>
</p>
</form>
@endsection
Step 9: Create a Dashboard Page
📄 resources/views/dashboard.blade.php
@extends('layouts.app')
@section('title', 'Dashboard')
@section('content')
<h2>Hello, {{ Auth::user()->name }}</h2>
<p class="text-center text-muted">Welcome to your dashboard!</p>
<form method="POST" action="{{ route('logout') }}" class="text-center">
@csrf
<button class="btn btn-danger mt-3">Logout</button>
</form>
@endsection
How to run Laravel Sing in and Signup Script
Laravel 12 requires PHP 8.2 or higher.
1. Download the zip file
2. Extract the file, copy the userauth folder, and paste it into xampp/htdocs
3. Open the command prompt, run these commands
cd /
cd C:\xampp\htdocs\userauth
4. Run the migration
php artisan migrate
5. Run the Application
php artisan serve
Open:
Register: http://127.0.0.1:8000/register
Login: http://127.0.0.1:8000/login
Dashboard: http://127.0.0.1:8000/dashboard
