I'm currently working on my first laravel project and i'm facing a problem.
If you have experience with laravel you probably know that by calling php artisan make:auth
you will get a predefined mechanism that handles login and registration.
This mechanism is set to understand a couple of commonly used words in order to automate the whole procedure.
The problem that occurs in my case is that i'm using oracle db and it won't let me have a table column with the name of password
because its a system keyword and it throws errors when trying to insert a user.
So far, i've tried to change my password
column to passwd
and it worked in my registration form as expected. The User row was successfully inserted and my page was redirected to /home.
But when i try to logout and then relogin, i get this error telling me that my credentials are not correct.
As for my code, i've changed my RegisterController.php
so that it takes username instead of email
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|max:50|unique:ECON_USERS',
'passwd' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'passwd' => bcrypt($data['passwd'])
]);
}
The User $fillable
protected $fillable = [
'username', 'passwd'
];
I'm guessing that Auth is trying to authenticate with email
and not username
or that Auth is searching for password
and not passwd
For having username
instead of email
, you can overwrite username() in your LoginController.php
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'username';
}
And for passwd
instead of password
, you can do define an accessor in your App\User.php
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->passwd;
}
login.blade.php : Replace email
input with username
but do not change the name of the input for password
.
In the app/Http/Controllers/Auth/LoginController
override the default class by adding:
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required', 'passwd' => 'required',
]);
}
Don't forget to add use Illuminate\Http\Request;
It could be you have to add this too to your LoginController
.
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'passwd');
}
That should do it.
Use this. It's work for me.
So far I have changed the User.php
public function getAuthPassword(){
return $this->senha;
}
and
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
Also on LoginController.php
public function username()
{
return 'usuario';
}
In your AuthController.php (Located in app/http/Controllers/Auth)
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$credentials = ($request->only('username', 'password'));
if ($this->auth->attempt($credentials)) {
//do something, credentials is correct!
}
return "Ops! snap! seems like you provide an invalid login credentials";
}