in my Laravel 5.2 project, I have user table which has a status field having 3 values. The values are , 0=not active, 1=active, 2 = paid. After login I want to check whether the user is having statuses 1 or 3. If they belongs to status 1 or 3 then I need to redirect to dashboard. Else nothing. The below is my getcredential method in AuthController.
protected function getCredentials($request) {
return $request->only($this->loginUsername(), 'password') + ['status' => 1];
}
How can I check both statuses here. Thanks in advance.
The value for a credential does not need to be a static value, you can also provide a Closure
that will be executed by the user provider on the query it uses to check credentials.
The following should do the trick:
protected function getCredentials($request) {
return $request->only($this->loginUsername(), 'password') + ['status' => function($query) {
$query->whereIn('status', [1, 3]);
}];
}
I found a solution for this problem. I kept the getCredential method as it is.
protected function getCredentials($request) {
return $request->only($this->loginUsername(), 'password') +
['status' => 1];
}
And I wrote the following code in login function in AuthController.
if($user->status == 3){
if(Auth::attempt(['email' => $request['email'], 'password' => $request['password'], 'status' => 3,]))
{
$fallback_url = '/dashboard';
$intended_url =
Redirect::intended($fallback_url)->getTargetUrl();
return response()->json(['auth' => true, 'intended' => $intended_url]);
}
}
And now users with status = 1 and status =3 can login to the dashboard.