Hi folks! After a day of *#!$", DispatchCode and I have realized a working installation of Nova4 with Auth0 support. Here's our conclusions, if you want to try it :)
Create a file in \app\Auth\CustomUserRepository.php and add the following code:
<?php
namespace App\Auth;
//...
class CustomUserRepository implements \Auth0\Laravel\Contract\Auth\User\Repository
{
public function fromSession(array $user): ?\Illuminate\Contracts\Auth\Authenticatable
{
$user = cache()->remember('user_logged_' . $user['email'], 60, function () use ($user) {
return \App\Models\User::firstOrCreate([
'email' => $user['email'],
], [
'name' => $user['name'],
//'password' => bcrypt('secret'), // if you want a fallback
'is_admin' => false,
]);
});
return $user;
}
public function fromAccessToken(
array $user
): ?\Illuminate\Contracts\Auth\Authenticatable {
// Simliar to above. Used for stateless application types.
return null;
}
}
This is useful if you don't want to see the standard login page after login (maybe there's a better option?).
In routes/web.php:
Route::get('/cms/login', function () {
return redirect('/');
});
(optional, see 4) Also add an endpoint if you want to protect whole website:
Route::get('/', [HomeController::class, 'index'])->name('home');
Skip if you don't want to protect whole website
Create HomeController
$ php artisan make:controller HomeController
then add:
<?php
namespace App\Http\Controllers;
class HomeController extends Controller
{
public function index(): \Illuminate\Http\RedirectResponse
{
if (auth()->check()) {
return redirect('/cms');
}
return redirect(route('login'));
}
}
In app\Models\User.php add the following:
<?php
use Auth0\Laravel\Contract\Model\Stateful\User as StatefulUser;
class User extends Authenticatable implements StatefulUser
...
public function canImpersonate()
{
//return Gate::forUser($this)->check('viewNova');
return false; // <- Impersonate (cannot?) works with Auth0
}
In config/auth.php assign the repository at auth0 provider:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'auth0' => [
'driver' => 'auth0',
'repository' => App\Auth\CustomUserRepository::class // <- Here!
'model' => App\Models\User::class, // <- Also here!
],
],
We got an error with the Util::class. Maybe is an isolated case, but if you got an error there's a temp workaround was to change the standard migration (you will need to publish the nova migrations):
In: database/migrations/2018_01_01_000000_create_action_events_table.php
Schema::create('action_events', function (Blueprint $table) {
$table->id();
$table->char('batch_id', 36);
$table->foreignIdFor(\App\Models\User::class)->index(); // <- Change this!
$table->string('name');
$table->morphs('actionable');
$table->morphs('target');
$table->string('model_type');
This is great! I was pulling my hair out trying to get this working ❤️
Have you gotten this to work with the event actions table?
use Laravel\Nova\Actions\Actionable;
It only seems to work if the default guard in auth.php is set for web, but this causes a login redirect loop. Changing it to auth0 allows the login to work but will throw an error for the Actions table:
[2022-12-12 22:13:07] local.ERROR: Class name must be a valid object or a string {"userId":1,"exception":"[object] (Error(code: 0): Class name must be a valid object or a string at /Users/Sites/matrix/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php:775)