Skip to content

Instantly share code, notes, and snippets.

@Kryptonit3-zz
Last active August 29, 2015 14:23
Show Gist options
  • Save Kryptonit3-zz/beeed274d2c0b5c209ed to your computer and use it in GitHub Desktop.
Save Kryptonit3-zz/beeed274d2c0b5c209ed to your computer and use it in GitHub Desktop.
Laravel 5 - Allow user to login with either username or email
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
public function loginPost(Request $request)
{
$useremail = $request->input('useremail');
$password = $request->input('password');
$remember = $request->input('remember');
// Check if user is using email or username
$field = filter_var($useremail, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$credentials = [
$field => $useremail,
'password' => $password,
];
// check if user is authentic
if (auth()->attempt($credentials, $remember)) {
// check if email has been verified
if (!auth()->user()->verified()) {
auth()->logout();
session()->flash('error', 'You must verify your email before you can access the site. ' .
'<br>If you have not received the confirmation email check your spam folder. ' .
'<b><a class="alert-link" href="' . route('resend.email') . '" class="alert-link">Click here</a></b> for the option to resend.');
return redirect()->route('home');
}
//event(new UserHasLoggedIn(auth()->user()));
session()->flash('success', 'Successfully logged in!');
return redirect()->intended(route('home'));
}
session()->flash('error', 'Your [Username/Email] and/or Password is incorrect!');
return redirect()->back()->withInput();
}
// ...
}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="well">
{!! Form::open(['route' => 'login', 'class' => 'form-horizontal']) !!}
<fieldset>
<legend>Login</legend>
<div class="form-group @if ($errors->has('useremail')) has-error @endif">
<div class="col-md-12">
<label class="control-label">Username or Email</label>
{!! Form::text('useremail', null, ['class' => 'form-control', 'placeholder' => 'Username or Email', 'required' => '']) !!}
@if ($errors->has('useremail'))
<span class="help-block">{{ $errors->first('useremail') }}</span>
@endif
</div>
</div>
<div class="form-group @if ($errors->has('password')) has-error @endif">
<div class="col-md-12">
<label class="control-label">Password</label>
{!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'Password', 'required' => '']) !!}
@if ($errors->has('password'))
<span class="help-block">{{ $errors->first('password') }}</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="checkbox">
<label>
<input name="remember" type="checkbox"> Remember me?
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-sm btn-success">Login</button>
</fieldset>
{!! Form::close() !!}
</div>
</div>
</div>
Route::get('/', ['as' => 'home', 'uses' => 'HomeController@index']);
Route::group(['middleware' => 'guest'], function () {
Route::get('login', ['as' => 'login', 'uses' => 'Auth\AuthController@login']);
Route::post('login', ['uses' => 'Auth\AuthController@loginPost']);
});
@RobinMalfait
Copy link

What if your username is an email address but different than your email?

@bbashy
Copy link

bbashy commented Jul 2, 2015

This is quite messy. You're doing more queries than you need to?

@Kryptonit3-zz
Copy link
Author

@bbashy - how is that? Didn't notice I was running the same query twice. The reason I do not implement the verified check in the credentials array as an additional column check is because I want to see if the login failed for failed password, or because they are not verified if you were wondering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment