Skip to content

Instantly share code, notes, and snippets.

@Bodom78
Last active January 1, 2018 11:25
Show Gist options
  • Save Bodom78/a046363eeb79b6066119 to your computer and use it in GitHub Desktop.
Save Bodom78/a046363eeb79b6066119 to your computer and use it in GitHub Desktop.
Use the Sentinel 2.x authentication package with Laravel 5.1 and MongoDB
Work in Progress: More testing to be done.
At a glance Authentication, Activation, Throttle, Groups & Permissions seem to
be working.
Downside: Copy and modify files.
Upside: Not really much work to get it up and running.
Assumes you have Laravel 5.1, jenssegers/laravel-mongodb + session addon set up
and your app is namespaced to App/
Install Sentinel 2.x and publish the configuration as per their instructions.
Because Sentinel models use the default Eloquent Model, we will need to copy
these and modify to use the MongoDB Model.
Copy the following vendor model files
-------------------------------------------------------------------------
\vendor\cartalyst\sentinel\src\Activations\EloquentActivation.php
\vendor\cartalyst\sentinel\src\Persistences\EloquentPersistence.php
\vendor\cartalyst\sentinel\src\Reminders\EloquentReminder.php
\vendor\cartalyst\sentinel\src\Roles\EloquentRole.php
\vendor\cartalyst\sentinel\src\Throttling\EloquentThrottle.php
\vendor\cartalyst\sentinel\src\Users\EloquentUser.php
To your application folder
-------------------------------------------------------------------------
\app\Sentinel\Activations\EloquentActivation.php
\app\Sentinel\Persistences\EloquentPersistence.php
\app\Sentinel\Reminders\EloquentReminder.php
\app\Sentinel\Roles\EloquentRole.php
\app\Sentinel\Throttling\EloquentThrottle.php
\app\Sentinel\Users\EloquentUser.php
In each of the Model files copied over
-------------------------------------------------------------------------
CHANGE
namespace Cartalyst\Sentinel\...;
TO
namespace App\Sentinel\...;
In each of the Model files copied over
-------------------------------------------------------------------------
CHANGE
use Illuminate\Database\Eloquent\Model;
TO
use Jenssegers\MongoDB\Model;
In the EloquentPersistence.php file add
-------------------------------------------------------------------------
use Cartalyst\Sentinel\Persistences\PersistenceInterface;
In the EloquentRole.php file add
-------------------------------------------------------------------------
use Cartalyst\Sentinel\Roles\RoleInterface;
In the EloquentUser.php file add
-------------------------------------------------------------------------
use Cartalyst\Sentinel\Users\UserInterface;
Update Sentinel Config
-------------------------------------------------------------------------
Now update the published Sentinel config so that all references to models
are using the copied files in you app, example for the user config option:
CHANGE
'model' => 'Cartalyst\Sentinel\Users\EloquentUser'
TO
'model' => 'App\Sentinel\Users\EloquentUser'
At this stage Login/Out will be working if the activation and throttle
checkpoints are disabled in the configuration. To get the checkpoints
working we need to modify the ServiceProvider and copy/modify some
Repository files.
Copy the following vendor files
-------------------------------------------------------------------------
\vendor\cartalyst\sentinel\src\Activations\IlluminateActivationRepository.php
\vendor\cartalyst\sentinel\src\Throttling\IlluminateThrottleRepository.php
\vendor\cartalyst\sentinel\src\Laravel\SentinelServiceProvider.php
To your application folder
-------------------------------------------------------------------------
\app\Sentinel\Activations\IlluminateActivationRepository.php
\app\Sentinel\Throttling\IlluminateThrottleRepository.php
\app\Sentinel\Laravel\SentinelServiceProvider.php
In the IlluminateActivationRepository.php file
-------------------------------------------------------------------------
CHANGE
namespace Cartalyst\Sentinel\Activations;
TO
namespace App\Sentinel\Activations;
ADD
use Cartalyst\Sentinel\Activations\ActivationRepositoryInterface;
Modify the create method by adding
$activation->completed = false; before the save.
In the IlluminateThrottleRepository.php file
-------------------------------------------------------------------------
CHANGE
namespace Cartalyst\Sentinel\Throttling;
TO
namespace App\Sentinel\Throttling;
ADD
use Cartalyst\Sentinel\Throttling\ThrottleRepositoryInterface;
In the SentinelServiceProvider.php file
-------------------------------------------------------------------------
CHANGE
namespace Cartalyst\Sentinel\Laravel;
TO
namespace App\Sentinel\Laravel;
CHANGE
use Cartalyst\Sentinel\Activations\IlluminateActivationRepository;
TO
use App\Sentinel\Activations\IlluminateActivationRepository;
CHANGE
use Cartalyst\Sentinel\Throttling\IlluminateThrottleRepository;
TO
use App\Sentinel\Throttling\IlluminateThrottleRepository;
In the prepareResources method update the paths to the vendor folder to avoid errors.
CHANGE
'/../config/config.php'
TO
'/../../../vendor/cartalyst/sentinel/src/config/config.php'
CHANGE
'/../migrations'
TO
'/../../../vendor/cartalyst/sentinel/src/migrations'
Use the copied and modified Service Provider. In your app config providers
-------------------------------------------------------------------------
CHANGE
'Cartalyst\Sentinel\Laravel\SentinelServiceProvider'
TO
'App\Sentinel\Laravel\SentinelServiceProvider'
Now Authentication, Activation, Throttle, Groups & Permissions should be working.
Remember to add some unique constraints to the roles->slug and users->email or whatever
identifier you go with.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment