if (Auth::attempt(['username' => $username, 'password' => $password]))
{
return redirect()->intended('dashboard');
}
Usernames for login?
I've added a username column to my users table and would like to use it for login instead of email.
What's the best way to implement this? I know I can easily change the AuthController.php and AuthenticatesAndRegistersUsers.php files, but I obviously don't want to overwrite Illuminate files.
Thanks!
You can always create your own AuthController and do whatever you need to do there ;) The example of RachidLaasri should help you to get started on that controller
For the default l5 install follow the use files in the auth controller. You see the functions and methods used and change as required.
The auth controllers and middlewares that ship with a stock laravel install are not part of the illuminate framework. You can safely change, or completely do away with them.
@willvincent Not sure what your post means, if you need to change the auth attempt variables the default auth setup refers to the core laravel files under the vendor folder.
What I mean is precisely what I said.
/App/Http/Controllers/Auth/AuthController.php
/App/Http/Controllers/Auth/PasswordController.php
/App/Http/Middleware/Authenticate.php
/App/Services/Registrar.php
/App/User.php
None of those are part of the illuminate framework, they're boilerplate code that laravel installs by default, but you can remove or change them as you want without significant worry.
So if you want to change the default user model that ships with laravel, and the default auth behavior, you'd need to update the user table migration to reflect the fields you want for that table. Update the User model (User.php) as needed, and update the validate and create methods in the Registrar.php to reflect changes to default fields.
None of that involves overwriting "illuminate files" ... my comment was specifically in response to @nitrammit saying:
but I obviously don't want to overwrite Illuminate files.
Those files do, however, ship with the Laravel framework. If you were to do a composer update and one or more of those files changed, in addition to any changes you've made, you might end up with merge conflicts.
There's no reason why you can't extend or re-implement those files, though.
@nitrammit simply override the trait method postLogin inside your controller:
use AuthenticatesAndRegistersUsers;
.....
...........
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required', 'password' => 'required',
]);
$credentials = $request->only('username', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('username', 'remember'))
->withErrors([
'username' => 'These credentials do not match our records.',
]);
}
Usman.
As far as I'm aware composer update won't change any of the code other than what's in the vendor dir. Am I wrong?
My bad. I overlooked the AuthController. Still learning!
Thanks for the help.
:)
You're right @willvincent, not a composer update. My mistake!
@willvincent good point, thanks for clarifying.
@usman is right, you have to override that method from the AuthenticatesAndRegistersUsers trait. That one is inside the vendor dir and not something you want to change directly.
Please or to participate in this conversation.