billycat
billycat

Reputation: 123

How to change the default behavior of Laravel 5.2 authentication

In the document of Laravel 5.2 "Authentication Quickstart":

"Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command".

php artisan make:auth


After run the command, the default behavior will create many files about Model,Controller,View, Database Tables etc…

I found that a "users" table is created, with "id name email password" columns, it's great!

How ever, if my web application has different structure about "user", such as I do not use the unique "email" property to identify.

How can I change the behavior of the command php artisan make:auth?

Thanks!

Upvotes: 0

Views: 451

Answers (2)

Taylor
Taylor

Reputation: 3141

The users table migration is there even before you do php artisan make:auth. So all you have to do to remove the unique key from your email column is to go to the migration file which is located here:

database/migrations/create_users_table.

And to remove the unique key from your email column, simply remove it from code as such

$table->string('email');

So it should look like this:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
});

Upvotes: 1

devnull
devnull

Reputation: 1928

Update your migrations as @Taylor Swift said.

As you mentioned. You don't have "email" in your user table.

Update your user migration to the following,

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
});

AuthController by default uses email to identify users

The laravel AuthController out-of-the box allow users to login using email, password.

Since you don't need the email. (Assuming you're using id as unique value for each users). You need to tell the AuthController to look for users by id rather than email.

So open your AuthController which is in

App\Http\Controllers\Auth\AuthController

and add the following line

protected $username = 'id';

Your Auth controller will look like

class AuthController extends Controller
{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    protected $username = 'id';


    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

}

Upvotes: 1

Related Questions