Laravel

Status
Not open for further replies.
Create a login form with the following functions:

1. If I try to login a credential, it checks the database if its role value is admin or user. If the value is admin then it will redirect me to admin dashboard and if the role value is user then it will redirect me to the user dashboard.

2. Create a register form within the login form.

3. Always hash passwords.
 
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.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 232
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
636
Guests online
1,260
Total visitors
1,896

Forum statistics

Threads
2,274,090
Posts
28,953,622
Members
1,235,061
Latest member
ekaj_meiho
Back
Top