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

前端 未结 7 1669
遇见更好的自我
遇见更好的自我 2020-11-27 06:08

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

相关标签:
7条回答
  • 2020-11-27 06:26

    If you are using the latest version. I am returning it like the following code below for a custom password field. On my end, I am using Lumen 6.x though it would apply to current version of Laravel also.

    /**
     * Get the custom password field for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->custom_password_field_here;
    }
    
    0 讨论(0)
  • 2020-11-27 06:26

    IN Custom Controller

    public function login(Request $request){
    
        if (Auth::attempt(['email' => $request->email,'password' => $request->password], false)){
    
             return redirect()->intended(route('subportal.dashboard'));
        }
    
        return $this->sendFailedLoginResponse($request);
    }
    
    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required', 'password' => 'required',
        ]);
    }
    
    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'email';
    }
    

    In App/Users.php

    public $table = "customer";
    protected $primaryKey = 'cust_id';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'pass', 'email','record_date',
    ];
    
    public function getAuthPassword() {
        return $this->pass;
    }
    
    0 讨论(0)
  • 2020-11-27 06:28

    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.

    0 讨论(0)
  • 2020-11-27 06:28

    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.

    0 讨论(0)
  • 2020-11-27 06:29

    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";
    
    }
    
    0 讨论(0)
  • 2020-11-27 06:34

    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';
    }
    
    0 讨论(0)
提交回复
热议问题