Laravel Reset Password using different column name

I managed to get it to work when it comes to authorization with different columns, name or username, email or password.

How to change custom password field name for user authentication Laravel 4 and Laravel 5

however, the password reminder doesn't seem to work.

I changed user model, my table column name.

User Model:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $primaryKey = 'user_id';
    protected $table = 'user';

    public function getReminderEmail() {
        return $this->user_email;
    }

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

    public function getRememberTokenName() {
        return 'user_token';
    }

}

      

Custom controller

Auth::attempt(  array(
 'user_name'    => $username, 
 'password'     => $password
), TRUE );

      

Reminder Controller // Error: Email address not found (Not sure if it is not reading user model getReminderEmail () )

public function post_reminder() {

        switch ($response = Password::remind(Input::only('email'))) {

            case Password::INVALID_USER:
                return Redirect::back()->with('error', Lang::get($response));

            case Password::REMINDER_SENT:
                return Redirect::back()->with('status', Lang::get($response));
        }


}

      

+3


source to share


2 answers


Took my watch to figure it out. The Laravel official doesn't seem to provide the correct documentation for implementing a custom name field.

We just need to change the input field name the same to the table column name.



Field could be anything
<input type="email" name="user_email">

      

+3


source


I think we can add custom fields. Following are the steps I am going to add to custom fields.

1 - add input

a register-like field .

i.e. <input type="email" name="you_custome_field">

      

2 - open app /services/Registrar.php and register your custom way in create function.ie



public function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'you_custome_field' => $data['you_custome_field'],
        ]);
    }

      

3 - (optional) Open user model

app / User.php, register the field as guarded

or fillable

whatever you want.

I hope this works.

0


source







All Articles