Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
users
table with hashed password.Make sure you have a users
table.
Run migration:
php artisan migrate
You can create a controller using Artisan:
php artisan make:controller AuthController
namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; class AuthController extends Controller { public function showRegisterForm() { return view('auth.register'); } public function register(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|min:6|confirmed', ]); User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), ]); return redirect()->route('login')->with('success', 'Registration successful!'); } public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { $credentials = $request->validate([ 'email' => 'required|email', 'password' => 'required', ]); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->route('dashboard'); } return back()->withErrors([ 'email' => 'Invalid credentials', ]); } public function logout(Request $request) { Auth::logout(); $request->session()->invalidate(); $request->session()->regenerateToken(); return redirect()->route('login'); } public function index() { return view('dashboard'); } }
use App\Http\Controllers\AuthController; Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register'); Route::post('/register', [AuthController::class, 'register']); Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login'); Route::post('/login', [AuthController::class, 'login']); Route::post('/logout', [AuthController::class, 'logout'])->name('logout'); Route::get('/dashboard', [AuthController::class, 'index'])->middleware('auth')->name('dashboard');
<form action="{{ route('register') }}" method="POST"> @csrf <input type="text" name="name" placeholder="Name" required> <input type="email" name="email" placeholder="Email" required> <input type="password" name="password" placeholder="Password" required> <input type="password" name="password_confirmation" placeholder="Confirm Password" required> <button type="submit">Register</button> </form>
<form action="{{ route('login') }}" method="POST"> @csrf <input type="email" name="email" placeholder="Email" required> <input type="password" name="password" placeholder="Password" required> <button type="submit">Login</button> </form>
<h1>Welcome, {{ auth()->user()->name }}</h1> <form action="{{ route('logout') }}" method="POST"> @csrf <button type="submit">Logout</button> </form>
Output register:
Output login:
Output dashboard: