To achieve the functionalities you described, you can follow these steps to create a login form with user registration included in Laravel:
1.
Database Setup: First, set up your database table to store user information. You can create a migration to create a
users table with fields like
name,
email,
password, and
role.
2.
Routes: Define the routes in
web.php for login, registration, and dashboard redirections:
PHP:
Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('/login', 'Auth\LoginController@login');
Route::post('/register', 'Auth\RegisterController@register');
Route::group(['middleware' => 'auth'], function () {
Route::get('/admin/dashboard', 'AdminController@dashboard')->name('admin.dashboard');
Route::get('/user/dashboard', 'UserController@dashboard')->name('user.dashboard');
});
3.
Authentication Controllers: Create controllers for handling login and registration logic. You can use Laravel's built-in
Auth functionality for this.
4.
Login Form: Create a login form view (
login.blade.php) with an option for registration:
HTML:
<form method="POST" action="{{ route('login') }}">
@csrf
<input type="email" name="email" required>
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>
<a href="{{ route('register') }}">Register</a>
5.
Registration Form: Inside the registration form, ensure passwords are hashed before saving to the database:
HTML:
<form method="POST" action="{{ route('register') }}">
@csrf
<input type="text" name="name" required>
<input type="email" name="email" required>
<input type="password" name="password" required>
<input type="password" name="password_confirmation" required>
<button type="submit">Register</button>
</form>
6.
Authentication Logic: In the login controller, after successful authentication, check the user's role and redirect accordingly:
PHP:
public function login(Request $request)
{
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
$user = Auth::user();
if ($user->role === 'admin') {
return redirect()->route('admin.dashboard');
} elseif ($user->role === 'user') {
return redirect()->route('user.dashboard');
}
}
return redirect()->route('login')->with('error', 'Invalid credentials');
}
7.
Dashboard Controllers: Create separate controllers for admin and user dashboards to handle the logic specific to each role.
8.
Always Hash Passwords: In the registration controller, ensure the password is hashed before storing it in the database:
PHP:
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
]);
$validatedData['password'] = Hash::make($request->password);
$user = User::create($validatedData);
return redirect()->route('login')->with('success', 'Registration successful. Please login.');
}
By following these steps, you can create a login form with registration included in Laravel, ensuring passwords are always hashed and redirecting users based on their roles to the respective dashboards.