Technical Implementation
Unify Laravel Auth
To use this package, you must first obtain access to the Unify Composer repository. You must also have received a Unify Key.
Installation
- Add the Unify composer repository to your project's
composer.json
file.
{
"repositories": [
{
"type": "composer",
"url": "https://composer.getunify.com"
}
]
}
- Install the package by running the following command in your project directory.
composer require getunify/laravel-auth
During installation, you might be prompted by Composer to enter your repository credentials.
- After installation, open
config/auth.php
and change your default guard tounify
.
'defaults' > [
'guard' => 'unify',
'passwords' => 'users',
],
- Add your Unify Key to your
.env
file.
UNIFY_KEY=[REDACTED]
Usage
If a user is not logged in and visits a protected page, they are redirected to Unify and asked to log in first. After the user logs in, they automatically return to the protected page they intended to visit.
Require user authentication to view a page
Use the auth
middleware to protect a route with authentication.
// In your routes file, for example: routes/web.php
Route::get('/pages/example-page')->middleware('auth');
// In a controller, for example: app/Http/Controllers/FooController.php
class FooController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
}
Only allow a specific access level to view a page
Pages that require a certain access level can be protected with the unify.level
middleware.
// Only allow users with access level 1
Route::get('/pages/example-page')->middleware('unify.level:1');
// Only allow users with access level 0 or 2, but not 1
Route::get('/pages/example-page')->middleware('unify.level:0,2');
See the Laravel documentation to learn more about middleware:
Check if the user is logged in
Use Laravel's authentication helpers to determine if the user is logged in.
if (Auth::check()) {
// Run code when user is authenticated
}
if (Auth::guest()) {
// Run code when user is not logged in
}
Reading user information
Use Laravel's authentication helpers to retrieve user information.
The returned value from Auth::user()
is an instance of \Unify\LaravelAuth\Session\Profile
. If there is no user logged in, the returned value is null
.
You can type-hint
\Unify\LaravelAuth\Session\Profile
on controller methods to automatically get the user object injected by Laravel.
From the user object you can read information.
use Illuminate\Support\Facades\Auth;
$user = Auth::user();
Available properties
$user->id; // "be9f339215128334459eed3a7c6b40cf"
$user->initials; // "J.E."
$user->name; // "Jane Doe"
$user->firstName; // "Jane"
$user->lastName; // "Doe"
$user->formalName; // "J.E. Doe"
$user->indexableName; // "Doe, Jane"
$user->email; // "jane.e.doe@example.com"
$user->timezone; // "Europe/Amsterdam"
$user->locale; // "nl"
$user->onekeyId; // "WNLR00000000"
$user->registrations; // ["foo_number" => "TEST001UNIFY", "bar_number" => "TEST002UNIFY"]
$user->address->organization; // "Acme Inc."
$user->address->street; // "Example St."
$user->address->number; // "42"
$user->address->letter; // "a"
$user->address->postalCode; // "1010AB"
$user->address->city; // "Amsterdam"
$user->address->country; // "NL"
Available methods
$user->hasAccessLevel0(); // true | false
$user->hasAccessLevel1(); // true | false
$user->hasAccessLevel2(); // true | false
$user->isDeveloper(); // true | false
$user->isEmployee(); // true | false
$user->isAdministrator(); // true | false
Logging out
To end the user's session, have them visit the logout
route of your application.