mardi 10 septembre 2019

How to amend this Laravel AuthController login function to check if active column is 0?

I have a Laravel + VueJS Project using Passport for authentication.

I am wanting to set up email verification, where the user has to click a linked email to him to verify.

This part of the process has been completed.

In my users' table I have an activation_token column, which populates with the users' token upon registration, and an active column which is 0 by default until the user has been verified.

Upon the user clicking the link in the email, the activation_token is set to '' if valid, and the active column set to 1.

This part of the process as described above works correctly.

However, my problem is that my login function does not check to see if active is 0 - as if this is the case, refuse access.

The following login function is from my AuthController.

class AuthController extends Controller
{
    public function login(Request $request)
    {
            $http = new \GuzzleHttp\Client();

            try {

                $response = $http->post(config('services.passport.login_endpoint'), [

            'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => config('services.passport.client_id'),
                    'client_secret' => config('services.passport.client_secret'),
            'username' => $request->username,
                    'password' => $request->password,

                    ]
                ]);

                return $response->getBody();

            } catch (\GuzzleHttp\Exception\BadResponseException $e) {
                if ($e->getCode()  === 400) {
                    return response()->json('Invalid Request. Please enter a username or password', $e->getCode());
                } else if ($e->getCode()  === 401) {
                    return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
            }

            return response()->json('Something went wrong on the server.', $e->getCode());
        }
    }

I have been following a tutorial: Laravel Passport Auth But the login function here is different to mine so I am struggling to incorporate this snippet as suggested:

$credentials = request(['email', 'password']);
$credentials['active'] = 1;
$credentials['deleted_at'] = null;

  if(!Auth::attempt($credentials))
        return response()->json([
            'message' => 'Unauthorized'
        ], 401);

How do I edit my function above to check to see if active = 0 ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2N9HyWK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire