Home > other >  Laravel: 2 Registration Forms with Login
Laravel: 2 Registration Forms with Login

Time:11-11

I'm working in Laravel 8 and like to know what's the best approach for logging in with the 2 registration forms I have already set up. The forms are set up with just validation, but trying to figure out how to have a login for these different sign-up roles, which will go to 2 different dashboards.

This is where I'm at with my code...

web.php

// Profile
Route::get('/register/profile', [RegisterProfileController::class, 'index'])->name('register_profile');
Route::post('/register/profile', [RegisterProfileController::class, 'store']);

// Business
Route::get('/register/business', [RegisterBusinessController::class, 'index'])->name('register_business');
Route::post('/register/business', [RegisterBusinessController::class, 'store']);

RegisterProfileController.php (with just validation)

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RegisterIndividualController extends Controller
{
    public function index()
    {
        return view('auth.register_profile');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'firstname' => 'required|max:255',
            'lastname' => 'required|max:255',
            'username' => 'required|min:8|max:60|alpha_num',
            'email' => 'required|email|max:255',
            'phone' => 'required|digits:10',
            'city' => 'required|max:100',
            'state' => 'required',
            'zip' => 'required|digits:5',
            'password' => 'required|confirmed'
        ]
    }
}

RegisterBusinessController.php (with just validation)

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RegisterBusinessController extends Controller
{
    public function index()
    {
        return view('auth.register_business');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'businessname' => 'required|max:255',
            'firstname' => 'required|max:255',
            'lastname' => 'required|max:255',
            'username' => 'required|min:8|max:60||alpha_num',
            'businessemail' => 'required|email|max:255',
            'phone' => 'required|digits:10',
            'address' => 'required|max:255',
            'city' => 'required|max:100',
            'state' => 'required',
            'zip' => 'required|digits:5',
            'website' => 'required|url',
            'industry' => 'required',
            'password' => 'required|confirmed'
        ]
    }
}

I created the migration schema for each:

2021_10_26_011205_create_individuals_table.php

2021_10_26_011224_create_businesses_table.php

CodePudding user response:

If you want to use Laravel's built-in authentication system, then you can set up your own authentication guards, which would let you log in using your existing models (here's a Stack Overflow answer with an example of setting up authentication with multiple models). Once you have this set up, you could then use the relevant guard in your login controller:

// Get the business model
$business = Business::where('businessemail', $request->input('businessemail'))->firstOrFail();

// Verify the password
if (Hash::check($request->input('password'), $business->password)) {
    // Log in
    Auth::guard('business')->login($business);
} else {
    // Incorrect password
}

Additionally, since it looks like you're still in the early stages of development, I would recommend reconsidering your authentication model. Instead of having two completely different tables, and needing to set up two different authentication guards, I would recommend using the User model for both individuals and businesses, and adding an account_type column. You could then create a business_profiles table, which links to the users table via a user_id column, which would hold the business-only information (businessname, industry, and website).

  • Related