0

I build a SPA with vue.js 2 for the frontend and Laravel as Backend API. Now I try to implement a validation if the user failed to sign in more than three times.

So I added a column 'login_attempts' (int) for my user's table. My problem is that I don't know how to get the 'login_attempts' value with Eloquent ORM in my laravel backend.

Also, I want to increase/reset that value if further validation of the login attempt has failed/passed.

// UserController.php
public function signin(Request $request) {

    $credentials = $request->only('email', 'password');
    $email = $request->input('email');

    // Check if login attempts above 3
    // Here I need to get the 'login_attempts' value
    $loginAttempts = App\User::where('email', $email)->value('login_attempts');
    if($loginAttempts >= 3){
       // Response: To many attempts
    }

    try{
        // Some JWT  Auth validation
        }
    } catch (JWTException $e) {
        // Increase failed login counter
        // $attemptedUser->   <--- Here i need to increase the 'login_attempts' for the user with the email of the login request
        $attemptedUser->save();
    }
    // Some more validation and response
}

Thanks for any help.

1 Answer 1

1

Some tweaks to your original code

//Here you get the 'login_attempts' value 

$loginAttempts = App\User::where('email', $email)->select('login_attempts')->first();

Then in catch() as below

...

$loginAttempts->login_attempts += 1;
$loginAttempts->save();

This will increase the count.

Sign up to request clarification or add additional context in comments.

1 Comment

make sure to reset login_attempts to 0 on successful login.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.