I am new to Laravel and I have done a system with users. Authenticate users is done by "php artisan make:auth", I have a column in users table called activated; when activated = 0, I don't want a user to log in. A little help please? Sorry if this clue has been boring.
Asked
Active
Viewed 1,370 times
-1
-
Does this answer your question? [Login only if user is active using Laravel](https://stackoverflow.com/questions/31015606/login-only-if-user-is-active-using-laravel) – Mateusz Mar 30 '20 at 19:12
2 Answers
0
You will want to handle this as either using the AuthenticatesUsers trait or manually part of your AuthController by checking the response from your login form and returning an appropriate response:
<?php
namespace App\Http\Controllers;
use App;
use Illuminate\Auth\AuthManager;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
/**
* Class AuthController
* @package Speechlink\Http\Controllers\Auth
*/
class AuthController extends Controller
{
use AuthenticatesUsers;
/**
* Create a new authentication controller instance.
* @param AuthManager $auth
*/
public function __construct(AuthManager $auth)
{
$this->auth = $auth;
}
/**
* Deal with the login attempt (overrides the trait's postLogin method)
*
* @param Request $request
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$credentials = $request->only('username', 'password');
if ($this->auth->attempt($credentials)) {
// We authenticated so continue
return redirect('path/to/logged_in');
} else {
// We failed authentication so redirect somewhere else
return redirect('path/to/disabled_user');
}
}
}
Spholt
- 3,724
- 1
- 18
- 29
-1
i find out myself, i just add this to myLoginController.php
protected function credentials(Request $request)
{
$credentials = $request->only($this->username(), 'password');
$credentials['activated'] = 1;
return $credentials;
}
User45648
- 105
- 1
- 16