Reputation: 181
So today I tried to modify the default auth in my laravel project.
First of all: Composer (1.4.2) and Laravel (5.4.27) (meaning also all dependencies) are up to date. I verified this with:
composer self-update
composer update
Then I altered the users table via a migration:
Schema::table('users', function (Blueprint $users){
$users->string('login', 16)->unique()->after('id');
$users->string('real_name', 32)->after('email');
});
Schema::table('users', function(Blueprint $users){
$users->dropColumn([
'name'
]);
});
The important part of that is that I want to use 'login' instead of 'name'.
Next thing I did was modifying the User class as follows:
protected $fillable = [
'login',
'email',
'real_name',
'password'
];
protected $primaryKey = 'login';
public function getAuthIdentifierName()
{
return 'login';
}
And also the LoginController:
public function username()
{
return 'login';
}
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
'g-recaptcha-response' => 'required|captcha'
]);
}
Finally I changed the login view:
<div class="form-group{{ $errors->has('login') ? ' has-error' : '' }}">
<label for="login" class="col-md-4 control-label">Login</label>
<div class="col-md-6">
<input id="login" type="text" class="form-control" name="login" value="{{ old('login') }}" required autofocus>
@if ($errors->has('login'))
<span class="help-block">
<strong>{{ $errors->first('login') }}</strong>
</span>
@endif
</div>
</div>
Now I should have a working authentication, right? That's what I thought.
The login itself works but when it comes to reading the value of the field 'login' it fails - or something like this.
debug-bar told me the following:
The query which will be executed on login is:
select * from `users` where `login` = 'admin' limit 1
Which is loading all the data correctly except of 'login'. For some reason this field stays '0' (in auth web)
"user" => array:10 [
"id" => 1
"email" => "[email protected]"
"created_at" => "2017-06-16 03:08:43"
"updated_at" => "2017-06-16 03:08:43"
"login" => 0
"real_name" => "PolluX"
And when it finally comes to displaying the default home view the executed query is:
select * from `users` where `login` = '0' limit 1
I'm using XAMPP v3.2.2, PHP 7.1.1 and 10.2.6-MariaDB as my local dev environment on Windows 10. I should mention that I replaced the default shipped MariaDB of this XAMPP version with a fresh download of the latest stable MariaDB version. (because of an error of the migration thing)
I also checked multiple posts on the auth thing on StackOverflow, Laracasts and so on but didn't find any post that could've helped me.
e.g. https://laracasts.com/discuss/channels/laravel/change-name-column-to-username-in-auth
Upvotes: 2
Views: 1187
Reputation: 8385
Answer for OP.
Delete the code protected $primaryKey = 'login'
and getAuthIdentifierName()
method.
All you have to do is override (add) username()
method in the controller.
Note for migrations and how to do it right:
down()
method that muchSome material from creator & friends of Laravel regarding migrations can be heard in this podcast http://www.laravelpodcast.com/episodes/68236-episode-53-bigger-better around 30 minute mark.
Upvotes: 1