Skip to content

Commit c9fb267

Browse files
Merge pull request #50 from NottingHack/login-username-or-email
Login: allow both username and email to be used during login
2 parents 947657c + 7ed6e2e commit c9fb267

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

app/Http/Controllers/Auth/LoginController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use Illuminate\Http\Request;
56
use App\Http\Controllers\Controller;
67
use Illuminate\Foundation\Auth\AuthenticatesUsers;
78

@@ -36,4 +37,34 @@ public function __construct()
3637
{
3738
$this->middleware('guest', ['except' => 'logout']);
3839
}
40+
41+
/**
42+
* Get the login username to be used by the controller.
43+
* Overirden as you allow both username and email during login
44+
*
45+
* @return string
46+
*/
47+
public function username()
48+
{
49+
return 'login';
50+
}
51+
52+
/**
53+
* Get the needed authorization credentials from the request.
54+
* Overirden as you allow both username and email during login
55+
*
56+
* @param \Illuminate\Http\Request $request
57+
* @return array
58+
*/
59+
protected function credentials(Request $request)
60+
{
61+
$login = $request->get('login');
62+
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
63+
64+
return [
65+
$field => $login,
66+
'password' => $request->get('password'),
67+
];
68+
}
69+
3970
}

resources/views/auth/login.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
<h2>Log In</h2>
66

7-
<p>Enter your email address and password to log in.</p>
7+
<p>Enter your email address or username and password to log in.</p>
88

99
<form role="form" method="POST" action="{{ url('/login') }}">
1010
{{ csrf_field() }}
1111

1212
<div class="row">
13-
<label for="email" class="form-label">E-Mail</label>
13+
<label for="login" class="form-label">E-Mail or Username</label>
1414
<div class="form-control">
15-
<input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus>
15+
<input id="login" type="text" name="login" value="{{ old('login') }}" required autofocus>
1616

17-
@if ($errors->has('email'))
17+
@if ($errors->has('login'))
1818
<p class="help-text">
19-
<strong>{{ $errors->first('email') }}</strong>
19+
<strong>{{ $errors->first('login') }}</strong>
2020
</p>
2121
@endif
2222
</div>

0 commit comments

Comments
 (0)