Laravel: How can I change the default Auth Password field name

I am currently working on my first laravel project, and I ran into a problem.

If you have experience with laravel, you probably know that by invoking php artisan make:authyou will get a predefined mechanism that processes login and registration.

This mechanism is designed to understand a couple of commonly used words to automate the entire procedure.

The problem that occurs in my case is that I am using oracle db and this will not allow me to have a table column with a name passwordbecause it is a system keyword and it causes errors when trying to insert a user.

So far, I have tried to change the column passwordto passwd, and it worked in my registration form, as expected. The user line was successfully inserted and my page was redirected to / home. registration

Success

But when I try to log out and then rewrite, I get this error telling me that my credentials are incorrect. enter image description here

As for my code, I changed mine RegisterController.phpso that instead of the email name, instead of the email name, the username

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'])
    ]);
}

Custom $ fillable

protected $fillable = [
    'username', 'passwd'
];

I assume Auth is trying to authenticate using email, not usernameor what Auth is looking for password, notpasswd

+4
source share
4 answers

username email () LoginController.php

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username';
}

passwd password accessor \. PHP

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->passwd;
}

login.blade.php: email username, password.

+8

app/Http/Controllers/Auth/LoginController , :

/**
 * 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',
    ]);
}

use Illuminate\Http\Request;

, 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');
    }

.

+1

. .

User.php

public function getAuthPassword(){  
    return $this->senha;
}

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

LoginController.php

public function username()
{
    return 'usuario';
}
+1

In your AuthController.php (located in the application / 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";

}
0
source

Source: https://habr.com/ru/post/1653931/


All Articles