PHP code example of faithfm / laravel-auth0-pattern
1. Go to this page and download the library: Download faithfm/laravel-auth0-pattern library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
'defined_permissions' => [
'use-app', // minimum permission to use the app
'admin-app', // master admin privilege
// 'edit-catalog', // for catalog editors (assuming you're writing a catalogue application)
],
// Check a user is logged in - using a variety of methods (default guard is our session-based Auth0 guard)
$loggedIn = auth()->check(); // using helper function (default guard)
$loggedIn = Auth::check(); // using Facades (default guard)
$loggedIn = auth('ffm-session-guard')->check(); // session-based guard specified - ie: using Auth0
$loggedIn = auth('ffm-token-guard')->check(); // token-based guard specified - ie: api_token=XXX
$loggedIn = Auth::guard('ffm-token-guard')->check(); // token-based guard specified using Facades
// Various useful guard functions - using the helper function and the default guard
auth()->user() // Eloquent model for the logged-in User
auth()->id() // User ID for this user - ie: user()->getAuthIdentifier()
auth()->check() // TRUE if logged in
auth()->guest() // FALSE if logged in
auth()->authenticate() // throw AuthenticationException if not logged in (similiar to middleware checks)
// Protect routes using 'auth' (Authenticate) middleware - throw AuthenticationException if not logged in
Route::get(...) { ... }
->middleware('auth'); // using default guard
->middleware('auth:ffm-token-guard'); // specifying token-based guard
->middleware('auth:ffm-session-guard,ffm-token-guard'); // allow EITHER session-based OR token-based guards
Route::get('/showuser', function () {
return auth()->user(); // session-based or token-based guard will automatically be used depending on first authenticated guard found during middleware check
})->middleware('auth:ffm-session-guard,ffm-token-guard');
$allowed = Gate::allows('use-app'); // simple test
$allowed = Gate::allows('use-app|edit-assets'); // ORed permissions (SPECIAL FEATURE)
...->middleware('can:use-app'); // protect in route definitions
Gate::authorize('use-app|admin-app'); // protect elsewhere (ie: controllers)
if (Gate::none(['use-app', 'admin-app'])) { // ditto - but manually aborting (instead of relying on "401 vs redirect" recommendation - see installation.md)
abort(403);
}
@can('use-app') // blade templates
if (Gate::allows('use-app'))
if (auth()->user()->permissions->restrictions['file'] == 'restrictedfile')
// ALLOW/DENY STUFF FROM HAPPENING
$LaravelAppGlobals = [
'user' => auth()->user(), # THIS IS THE IMPORTANT ONE
'guest' => auth()->guest(),
'other-stuff' => $myStuff,
...
];
return view('media')->with('LaravelAppGlobals', $LaravelAppGlobals);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.