error logging in with user with different role to admin

0

I have an api rest made in laravel 5.6 where I authenticate and register with passport and oauth, I register successfully but I am having problems logging in with users that do not have administrator role, it tells me they are not authorized.

On the front I have no problems, I verify and if the login, email and password data are sent, they even pass the validation, the problem is that in my login function:

public function login(Request $request)
{   
    $credentials = [
        'email' => $request->email,
        'password' => $request->password
    ];

    if (auth()->attempt($credentials)) {
        $token = auth()->user()->createToken('TutsForWeb')->accessToken;
        return response()->json(['token' => $token], 200);
    } else {
        return response()->json(['error' => 'UnAuthorised'], 401);
    }
}

When the user has a role other than admin, the auth () -> attempt ($ credentials) statement becomes null and that is why I get error 401, I do not understand why, when logging in with a user with administrator role, It works correctly.

    
asked by DVertel 27.07.2018 в 17:40
source

1 answer

0

I have my doubts about whether it is a problem with the attempt () method, since this method only checks that the parameters you send are identical to those in the database .. Try the following:

try the following:

dd (Auth :: attempt ($ credentials)), and see if it really returns a null .. if so, verify that the data you're passing is really the right one.

My User Model:

protected $fillable = [
    'nombre','email', 'password','permiso'
];

LoginController:

if (Auth::attempt(array('email' => $request->email, 'password' => $request->password),$request->_token))

Try it, I can not think of anything else

luck.

    
answered by 27.07.2018 в 21:25