Force lowercase on email to prevent capitalization issue with SQLite

This commit is contained in:
Bubka 2020-12-02 23:48:16 +01:00
parent 2f728a7980
commit 4d6ae849d8
3 changed files with 69 additions and 5 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Http\Response;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use App\Http\Requests\CaseInsensitiveLogin;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Carbon\Carbon; use Carbon\Carbon;
@ -35,9 +36,8 @@ class LoginController extends Controller
* *
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
*/ */
public function login(Request $request) public function login(CaseInsensitiveLogin $request)
{ {
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle // 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 login attempts for this application. We'll key this by the username and

View File

@ -0,0 +1,64 @@
<?php
namespace App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Http\FormRequest;
class CaseInsensitiveLogin extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => [
'required',
'email',
function ($attribute, $value, $fail) {
if ('sqlite' === config('database.default')) {
$user = DB::table('users')
->whereRaw('email = "' . $value . '" COLLATE NOCASE')
->first();
}
else {
$user = DB::table('users')
->where('email', $value)
->first();
}
if (!$user) {
$fail(__('validation.custom.email.exists'));
}
},
],
'password' => 'required|string',
];
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
$this->merge([
'email' => strtolower($this->email),
]);
}
}

View File

@ -51,11 +51,11 @@ class User extends Authenticatable
} }
/** /**
* Get Email attribute * set Email attribute
* @param string $value * @param string $value
*/ */
public function getEmailAttribute($value) public function setEmailAttribute($value)
{ {
return strtolower($value); $this->attributes['email'] = strtolower($value);
} }
} }