Anyone?
[5.3] Login via email OR username
Hi everyone,
In my application, I want to be able to login in either with my username or my email address (thus, I added a username column to the schema).
I tried to set up the LoginController like this, which did not work ...
public function username()
{
$login = request('login');
return filter_var( $login, FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
}
protected function validateLogin(Request $request)
{
$this->validate($request, [
'login' => 'required', 'password' => 'required',
]);
}
My login form contains the inputs login (which is the email or the username) and a password input.
... I think I need to implement something more. Can you help me?
Thanks in advance!!
I made it in Laravel 5.3.4, Can you try to read Chinese article? link:
http://river0314.lofter.com/post/1d03f335_c2a1605
or i translate one way to make it: In /app/Controllers/Auth/LoginController add 2 functions, copy from \vendor\laravel\framework\src\Illuminate\Foundation\Auth\ AuthenticatesUsers.php , they are credentials() and username() functions,they edit them like this:
/**
* add function credentials and username
* 添加这两个修改之后的方法,为了兼容email和mobile 两个字段作为账号登录
*/
protected function credentials(Request $request)
{
$field = filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$request->merge([$field => $request->input($this->username())]);
return $request->only($field, 'password');
}
/** 这个是声明账户登录的name值 */
public function username()
{
return 'login';
}
besides, edit login.blade.php like this: /** login */ Email/UserName
<div class="col-md-6">
<input id="email" type="text" class="form-control" name="login" value="{{ old('login') }}" autofocus>
@if ($errors->has('login'))
<span class="help-block">
<strong>{{ $errors->first('login') }}</strong>
</span>
@endif
</div>
</div>
so, you can use email or username to login.
so, you don't need to edit AuthenticatesUsers.php. Forgive my poor English
Please or to participate in this conversation.