Home > Net >  How to authenticate user using attempt method and status?
How to authenticate user using attempt method and status?

Time:10-17

I am using the attempt() method to authenticate user:

    if (!Auth::attempt($request->only('phone', 'password'))) {
        return response()->json([
            'message' => 'Invalid login details'
        ], 401);
    }

Problem is I need to check if the status field is true in the users table. How to do that?

I have read more the docs and found this:

  $credentials = ['phone' => $request->phone, 'password' => $request->password, 'status' => 1];

        if (!Auth::attempt($request->only($credentials))) {}

CodePudding user response:

You could follow this approach as well:

if (Auth::attempt($request->only('phone', 'password'))) {
   //logged in but account inactive
   if(!auth()->user()->status){
     return response()->json([
        'message' => 'Account Inactive'
     ],403);
   }

   //redirect to dashboard
   //user logged in
   return response()->json([
       //data you want to send
   ],403);
}

//login failed
return response()->json([
   'message' => 'Invalid login details'
], 401);
  • Related