php - Is it possible in LARAVEL to tag a user as logged in when getting his username from a custom URL?

I am currently doing a website wherein the login URLs are varying and displays the data according to the assigned projects to them.

For example, user A can only access www.example.com/projects/proj1. This is the homepage for user A and if he logs in he uses www.example.com/projects/proj1/login

While user B can only access www.example.com/projects/proj2. This is the homepage for user B and if he logs in he uses www.example.com/projects/proj2/login

Please note that proj1 and proj2 are varying depending on the database. So I have to check first that these projects are already registered in the database.

I am thinking of having a route like this.

For web.php

Route::get('/projects/{project_name}', 'PageHandler\CustomPageController@projects');
Route::get('/projects/{project_name}/login', 'PageHandler\CustomPageController@login');
Route::put('/projects/{project_name}/auth/{user}', 'PageHandler\TestUserPageController@auth');

Then my customepagecontroller.php looks like this

class CustomPageController extends Controller
{

    public function projects(string $projectName)
    {
        if (auth()->user() == null) 
            return redirect('/projects'. '/' . $projectName . '/login');
    }

    public function login(string $projectName)
    {
        return view('login')->with('projectName', $projectName);
    }

    public function auth(Request $request, string $projectName)
    {
        $username = $request->username;
        
        //How to set $username as logged in?

        // rest of the code to show the home page after authentication
    }
}

login.blade.php basically just looks like a form submitting username and password and calling auth of CustomPageController with a string parameter for the URL

So my question is how can I set $username as logged in already using the Auth of Laravel? Or should I create my custom Authentication Controllers?

Now, this is the only approach I have in mind for me to enable the logging in of users to varying URLs. Please let me know if you have better approach.

Thank you!

Answer

Solution:

If you only want to limit the project the users can access, I do not see a need to use 2 different login URLs (please correct me if there is a reason why you want different URLs for that), instead, you simply find which project the user belongs to from the database.

For authentication, Laravel allows you to implement authentication in a very easy way, you can refer to the documentation. Using Laravel's authentication would be easier and safer than writing your own one, and even if the default functionalities it provides may not be exactly the same as those you would want to achieve, you can still add your own things, which is still a lot easier than implementing it from scratch.

As for setting a user as logged in with Laravel's authentication services, you can use Auth::login($user);. Here, $user must be an implementation of the Illuminate\Contracts\Auth\Authenticatable contract. You can refer to this part of the documentation for more details.

Source