Skip to content

Login attempt modification with one another column

I want two users to use same mobile number and password, but they may have different role_id, which is a column (i added in users table), this role_id may have different value according to role of a user. lets assume 4 for manager, 5 for supervisor.

I want manager and supervisor to use same mobile number and password while login, is there a way i can pass a third parameter (role_id along with mobile_no and password) and laravel login attempt to checks that value and try to login that credentials from database.

I know we can add active (boolen) columns where we can pass a value that account should be active. But here role_id can have any value starting from 1.

So is there a way i can modify attempt function or check a where clause along with mobile number, password and role_id so that laravel try to login that credentials associated with that role_id.

What i tried, i tried to create two users with same mobile number and password, laravel is not logging either one of them, because it is not knowing which user to login, also different password is also not working for same mobile number

i am using laravel 5.8

Advertisement

Answer

You can code your own auth logic using the Auth facade. Give a look to the documentation: Manually authenticating users

So you would need to retrieve the roleid from somewhere to know what user it should attempt to login to, and in the controller you would have something like this:

// This is assuming that you're getting all the info from the form
// You can create your own array and set the information how you want.
$credentials = $request->only('mobile_number', 'password', 'role_id');

if (Auth::attempt($credentials)) {
    // Authentication passed
    // do what you want when the
    // user gives valid credentials
}

Note that we are also giving the role_id to the attempt method, meaning that it will try to find a match in the database for the mobile_number, the password and the role_id and retrieve that user.

User contributions licensed under: CC BY-SA
6 People found this is helpful