-
Notifications
You must be signed in to change notification settings - Fork 2
Auth
Laravel includes a comprehensive authentication implementation - documentation available here. This Doctrine2Bridge package includes a user provider which allows you to use Doctrine2 for your user database backend.
The following instructions will explain how to use this feature:
You need a database table to store your users and passwords. You can call this table anything you like. We include an example XML schema and generated entity in the examples/auth/
folder.
What you need to do is:
- create a table to hold your users;
- this table requires a column for usernames/emails and password;
- when this table is generated, add
implements \Illuminate\Auth\UserInterface
to the class definition; - implement the
getAuthIdentifier()
method which simply returns the object id; - implement the
getAuthPassword()
method which returns the (hashed) password.
In your published Doctrine2Bridge configuration (app/config/packages/opensolutions/doctrine2bridge/config.php
), update the authentication section such as:
'auth' => array(
'enabled' => true,
'entity' => '\Entities\User' // the Doctrine2 entity representing the user
)
The entity
element should be your Doctrine2 user entity class.
Now, update app/config/auth.php
and set the driver:
'driver' => 'doctrine2bridge',
That's it! You now have Doctrine2 / Laravel combined for authentication.
You can test this by adding a user - such as (at the top of app/routes.php
):
$hasher = new Illuminate\Hashing\BcryptHasher;
$u = new Entities\User();
$u->setUsername( 'testuser');
$u->setPassword( $hasher->make( 'qwerty89' ) );
D2EM::persist($u);
D2EM::flush();
Run your application (./artisan serve; wget http://localhost:8000/
), and check that the user has been created. Then remove the above code and replace it with authentication tests:
echo '<pre>';
echo 'Test failed authentication (result should be false): ';
var_dump( Auth::attempt( array('username' => 'testuser', 'password' => 'dontknow') ) );
echo 'Test successful authentication (result should be true): ';
var_dump( Auth::attempt( array('username' => 'testuser', 'password' => 'qwerty89') ) );
die();
And the expected output is:
Test failed authentication (result should be false): bool(false)
Test successful authentication (result should be true): bool(true)