Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

birdietorerik's avatar

Possible to select role on login

Hi!

I have a application made in laravel8.x and vue

Have this Logincontroller:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo;
    public function redirectTo()
    {
        //$rollen = Auth()->user();
        $redirects = [
            'Superadmin' => '/admin/dashboard',
            'player' => '/player/dashboard',
            'scout' => '/scout',
        ];

        $roles = Auth()->user()->roles->map->title;

        foreach ($redirects as $role => $url) {
            if ($roles->contains($role)) {
                return $url;
            }
        }
        return '/login';
         
    } 

    /**
     * Create a new controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * The user has logged out of the application.
     *
     * @return mixed
     */
    protected function loggedOut(Request $request)
    {
        if ($request->wantsJson()) {
            return response(null, Response::HTTP_NO_CONTENT);
        }
    }
}

What i want is:

If a user have more then 1 role, the user can select a role, before login.

Example:

role user
*********
Superadmin
player
scout

On login the user enter -> email, password and role (Superadmin,player,scout)

If example user select role->player The system login the user with role->player

How do i do this in laravel8.x/vue ?

0 likes
5 replies
Tray2's avatar

The easiest should be to add a dropdown where the user can select, but I think you need to consider as to why the user would need more roles in this case. A superadmin should both be scout and player.

birdietorerik's avatar

@Tray2 Hi!

User must be able to select more then one role Hav a dropdown with the roles, and Auth wil set this role on login

tykus's avatar

@birdietorerik rather than offering a dropdown on the login form (which unnecessarily exposes your roles to user who may never have more than one role); you could add a middleware which determines if a selection is necessary. For example, if the authenticated user has more than one role and there is no role in the session, display a form to allow the authenticated user to select their role for the current session; redirecting to the appropriate endpoint after the role is selected.

Please or to participate in this conversation.