Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate with Sentinel by username?

I'm using Sentinel by Cartalyst for authentication with PHP. The traditional way to authenticate is by use of e-mail and password, as we can read in their documentation:

$credentials = [
    'email'    => '[email protected]',
    'password' => 'password',
];

Sentinel::authenticate($credentials);

The default users table provided by the library is like that:

id | email | password | permissions |last_login | first_name | created_at | updated_at

I want to add a username column and authenticate using Sentinel by username and password. Is it possible?

like image 561
jtrezza Avatar asked Mar 17 '15 19:03

jtrezza


1 Answers

You can modify this example here

http://naomiaro.com/2015/07/08/multiple-login-attributes-with-sentinel-and-laravel/

In your Users model you must override the attribute $loginNames, and add username to the attribute $fillable.

use Cartalyst\Sentinel\Users\EloquentUser as SentinelUser;

class User extends SentinelUser {

    protected $fillable = [
        'email',
        'username',
        'password',
        'last_name',
        'first_name',
        'permissions',
    ];

    protected $loginNames = ['username'];

}

Sentinel::authenticate([
    'username'    => 'fake',
    'password' => 'password'
]);
like image 92
naomiaro Avatar answered Oct 19 '22 17:10

naomiaro