Am a Laravel Beginnner. So i want the user the be able to login with either the name or email.
The auth is generated via php artisan make:auth
.
Am using Laravel 5.2
-- EDIT -- And where should i put this ?
Am a Laravel Beginnner. So i want the user the be able to login with either the name or email.
The auth is generated via php artisan make:auth
.
Am using Laravel 5.2
-- EDIT -- And where should i put this ?
This is how we make email or username validation in Laravel 5.2, have a look at the code of my AuthController
for similar case
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
protected $username = 'username';
protected $maxLoginAttempts = 10;
protected $redirectPath = '/';
protected $redirectAfterLogout = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:syscare_users',
'password' => 'required|confirmed|min:6',
'username' => 'max:255|min:6|alpha_num|unique:syscare_users',
]);
}
/**
* Handle a login request to the application. - Overriding
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function login(Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (Auth::attempt(['username' => $request->username , 'password' => $request->password], $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
elseif(Auth::attempt(['email' => $request->username , 'password' => $request->password],$request->has('remember')))
{
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return redirect()->back()
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}
/**
* Get the failed login message. - Override
*
* @return string
*/
protected function getFailedLoginMessage()
{
return Lang::has('auth.failed')
? Lang::get('auth.failed')
: 'Invalid username/email or password.';
}
}