Skip to content

Laravel Fortify ​

Nguồn gốc: Bản dịch từ Laravel Fortify

Giới thiệu (Introduction) ​

Laravel Fortify là triển khai authentication backend frontend agnostic cho Laravel. Fortify đăng ký route và controller cần thiết để thực hiện tất cả tính năng authentication: login, registration, password reset, email verification, và nhiều hơn.

Vì Fortify không cung cấp giao diện riêng, nó được thiết kế để ghép nối với UI tùy chỉnh của bạn — giao diện gửi request đến các route mà Fortify đăng ký.

GỢI Ý

Fortify là package mà application starter kits sử dụng nội bộ. Nếu dùng starter kit, bạn không cần cài Fortify riêng.

Fortify là gì? ​

Fortify lấy route và controller từ starter kit và đóng gói thành package — cho phép scaffold nhanh phần backend authentication mà không ràng buộc frontend cụ thể.

Khi nào nên dùng Fortify? ​

Fortify vs. Sanctum ​

Sanctum quản lý API token và xác thực session — không xử lý registration, password reset.

Fortify xử lý authentication flows (registration, password reset...). Hai package bổ trợ chứ không thay thế nhau: dùng Fortify cho auth flows + Sanctum cho API token.

Cài đặt (Installation) ​

bash
composer require laravel/fortify

Publish resources:

bash
php artisan fortify:install

Lệnh này publish Actions vào app/Actions, FortifyServiceProvider, config file và database migrations.

Chạy migration:

bash
php artisan migrate

Fortify Features ​

Cấu hình trong config/fortify.php → mảng features để bật/tắt tính năng.

Disabling Views ​

Nếu xây dựng SPA, có thể tắt view response của Fortify.

Authentication ​

Customizing User Authentication ​

Tùy chỉnh logic xác thực user.

Customizing the Authentication Pipeline ​

Tùy chỉnh pipeline xác thực qua class request.

Customizing Redirects ​

Tùy chỉnh redirect sau login/logout.

Two-Factor Authentication (2FA) ​

Bật 2FA ​

Kích hoạt xác thực 2 yếu tố cho user, hiển thị QR code + recovery codes.

Xác thực với 2FA ​

Sau khi bật, user nhập TOTP code khi đăng nhập.

Tắt 2FA ​

Vô hiệu hóa xác thực 2 yếu tố.

Registration ​

Customizing Registration ​

Tùy chỉnh logic tạo user khi đăng ký.

Password Reset ​

Gửi email chứa link đặt lại mật khẩu.

Resetting the Password ​

Xử lý đặt lại mật khẩu từ link.

Email Verification ​

Protecting Routes ​

Bảo vệ route yêu cầu email đã xác thực.

Password Confirmation ​

Yêu cầu user xác nhận mật khẩu trước khi thực hiện hành động nhạy cảm.