Skip to content

Commit 73f4a31

Browse files
authored
Laravel 11 support (#16)
* Pin testbench version * Use redirectTo method and change default redirect to the welcome page * Defaults to Models directory * Switch to casts() method * Make the default controller empty and do not use middleware in controllers * Update fixtures to use new conventions * Use anonymous classes for migrations * Update installation documentation --------- Co-authored-by: avvertix <5672748+avvertix@users.noreply.github.com>
1 parent 0b3ae3f commit 73f4a31

31 files changed

+165
-187
lines changed

.github/workflows/ci.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ jobs:
2121
matrix:
2222
os: [ubuntu-latest, windows-latest]
2323
php: [8.3, 8.4]
24-
laravel: ["10.*"]
24+
laravel: ["11.*"]
2525
stability: [prefer-lowest, prefer-stable]
2626
include:
27-
- laravel: 10.*
28-
testbench: 8.*
27+
- laravel: 11.*
2928
carbon: ^2.63
3029

3130
steps:
@@ -53,7 +52,7 @@ jobs:
5352
5453
- name: Install dependencies
5554
run: |
56-
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update
55+
composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update
5756
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
5857
5958
- name: List Installed Dependencies

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,23 @@ configure the required options inside the `services` configuration file.
9191
'client_secret' => env('GITLAB_CLIENT_SECRET'),
9292
'redirect' => null, // set in the controller no need to specify
9393
'instance_uri' => env('GITLAB_INSTANCE_URI', 'https://gitlab.com/')
94+
// 'host' => env('GITLAB_INSTANCE_URI', 'https://gitlab.com/') // if using the default Socialite Gitlab driver
9495
],
9596
```
9697

98+
> [!TIP]
99+
> We do require also Socialite Providers Gitlab driver. So you need to include the `Identity::events()` in your app provider. If you want to use the Gitlab driver included in Laravel Socialite you can omitt `Identity::events()`. Remember to use `host` instead of `instance_uri` to configure the url of your Gitlab instance.
100+
97101
If you are using one of the community maintained [Socialite Providers](https://socialiteproviders.com/)
98-
remember to register their events in your `EventsServiceProvider`.
102+
remember to register their events in your `AppServiceProvider`.
99103

100104
If you are not using those providers this step is optional.
101105

102106
`oneofftech/laravel-connect-identity` provides out-of-the-box support for the `gitlab`
103107
and `dropbox` driver. If you are using those two you might add the following call to
104-
your `EventsServiceProvider`.
108+
your `AppServiceProvider`.
105109

106110
```php
107-
108111
public function boot()
109112
{
110113
parent::boot();

composer.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
],
1919
"require": {
2020
"php": "^8.2",
21-
"illuminate/auth": "^10.0",
22-
"illuminate/console": "^10.0",
23-
"illuminate/encryption": "^10.0",
24-
"illuminate/support": "^10.0",
25-
"illuminate/view": "^10.0",
21+
"illuminate/auth": "^11.0",
22+
"illuminate/console": "^11.0",
23+
"illuminate/encryption": "^11.0",
24+
"illuminate/support": "^11.0",
25+
"illuminate/view": "^11.0",
2626
"laravel/socialite": "^5.0",
2727
"socialiteproviders/gitlab": "^4.0",
2828
"socialiteproviders/dropbox": "^4.1"
@@ -33,7 +33,7 @@
3333
"larastan/larastan": "^2.9",
3434
"mockery/mockery": "^1.4.4",
3535
"phpunit/phpunit": "^10.5",
36-
"orchestra/testbench": "^8.22"
36+
"orchestra/testbench": "^9.9"
3737
},
3838
"autoload": {
3939
"psr-4": {

src/Auth/RedirectsUsers.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ trait RedirectsUsers
1010
{
1111
/**
1212
* Get the post register / login redirect path.
13-
*
14-
* @return string
1513
*/
16-
public function redirectPath()
14+
public function redirectPath(): string
1715
{
1816
if (method_exists($this, 'redirectTo')) {
1917
return $this->redirectTo();
2018
}
2119

22-
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
20+
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/';
2321
}
2422
}

src/Console/Commands/ScaffoldAuthenticationControllers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function identifyApplicationNamespace()
9797
$this->comment("Using [$this->namespace] as application namespace.");
9898
}
9999

100-
$this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\\Models' : $this->namespace;
100+
$this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'Models' : $this->namespace;
101101
} catch (RuntimeException $ex) {
102102
$this->warn("Unable to identity the application namespace, assuming [$this->namespace].");
103103
}

src/Facades/Identity.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class Identity extends Facade
2626
*
2727
* @var string
2828
*/
29-
public static $userModel = 'App\\User';
29+
public static $userModel = 'App\\Models\\User';
3030

3131
/**
3232
* The identity model that should be used.
3333
*
3434
* @var string
3535
*/
36-
public static $identityModel = 'App\\Identity';
36+
public static $identityModel = 'App\\Models\\Identity';
3737

3838
/**
3939
* Get the registered name of the component.
@@ -53,24 +53,34 @@ protected static function getFacadeAccessor()
5353
*/
5454
public static function routes()
5555
{
56+
/**
57+
* @var \Illuminate\Routing\Router
58+
*/
5659
$router = static::$app->make('router');
5760

5861
$namespace = '\\'.rtrim(self::$appNamespace, '\\');
5962

60-
$router->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect")
61-
->name('oneofftech::login.provider');
62-
$router->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login")
63-
->name('oneofftech::login.callback');
63+
$router
64+
->middleware('web')
65+
->group(function ($groupRouter) use ($namespace) {
66+
$groupRouter->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect")
67+
->name('oneofftech::login.provider');
68+
$groupRouter->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login")
69+
->name('oneofftech::login.callback');
70+
71+
$groupRouter->match(['get', 'post'], 'register-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\RegisterController@redirect")
72+
->name('oneofftech::register.provider');
73+
$groupRouter->get('register-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\RegisterController@register")
74+
->name('oneofftech::register.callback');
75+
76+
$groupRouter->match(['get', 'post'], 'connect-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\ConnectController@redirect")
77+
->middleware('auth')
78+
->name('oneofftech::connect.provider');
79+
$groupRouter->get('connect-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\ConnectController@connect")
80+
->middleware('auth')
81+
->name('oneofftech::connect.callback');
82+
});
6483

65-
$router->match(['get', 'post'], 'register-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\RegisterController@redirect")
66-
->name('oneofftech::register.provider');
67-
$router->get('register-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\RegisterController@register")
68-
->name('oneofftech::register.callback');
69-
70-
$router->match(['get', 'post'], 'connect-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\ConnectController@redirect")
71-
->name('oneofftech::connect.provider');
72-
$router->get('connect-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\ConnectController@connect")
73-
->name('oneofftech::connect.callback');
7484
}
7585

7686
/**

stubs/Identities/Auth/ConnectController.stub

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace App\Http\Controllers\Identities\Auth;
44

55
use App\User;
66
use App\Http\Controllers\Controller;
7-
use App\Providers\RouteServiceProvider;
87
use Oneofftech\Identities\Auth\ConnectUserIdentity;
98

109
class ConnectController extends Controller
@@ -25,18 +24,9 @@ class ConnectController extends Controller
2524

2625
/**
2726
* Where to redirect users after connection.
28-
*
29-
* @var string
3027
*/
31-
protected $redirectTo = RouteServiceProvider::HOME;
32-
33-
/**
34-
* Create a new controller instance.
35-
*
36-
* @return void
37-
*/
38-
public function __construct()
28+
protected function redirectTo(): string
3929
{
40-
$this->middleware('auth');
30+
return '/';
4131
}
4232
}

stubs/Identities/Auth/LoginController.stub

+2-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Http\Controllers\Identities\Auth;
44

55
use App\Http\Controllers\Controller;
6-
use App\Providers\RouteServiceProvider;
76
use Oneofftech\Identities\Auth\AuthenticatesUsersWithIdentity;
87

98
class LoginController extends Controller
@@ -24,18 +23,9 @@ class LoginController extends Controller
2423

2524
/**
2625
* Where to redirect users after login.
27-
*
28-
* @var string
2926
*/
30-
protected $redirectTo = RouteServiceProvider::HOME;
31-
32-
/**
33-
* Create a new controller instance.
34-
*
35-
* @return void
36-
*/
37-
public function __construct()
27+
protected function redirectTo(): string
3828
{
39-
$this->middleware('guest');
29+
return '/';
4030
}
4131
}

stubs/Identities/Auth/RegisterController.stub

+4-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace App\Http\Controllers\Identities\Auth;
44

5-
use App\User;
5+
use App\Models\User;
66
use Illuminate\Support\Str;
77
use App\Http\Controllers\Controller;
88
use Illuminate\Support\Facades\Hash;
9-
use App\Providers\RouteServiceProvider;
109
use Illuminate\Support\Facades\Validator;
1110
use Oneofftech\Identities\Auth\RegistersUsersWithIdentity;
1211

@@ -27,19 +26,10 @@ class RegisterController extends Controller
2726

2827
/**
2928
* Where to redirect users after registration.
30-
*
31-
* @var string
32-
*/
33-
protected $redirectTo = RouteServiceProvider::HOME;
34-
35-
/**
36-
* Create a new controller instance.
37-
*
38-
* @return void
3929
*/
40-
public function __construct()
30+
protected function redirectTo(): string
4131
{
42-
$this->middleware('guest');
32+
return '/';
4333
}
4434

4535
/**
@@ -68,7 +58,7 @@ class RegisterController extends Controller
6858
return User::create([
6959
'name' => $data['name'],
7060
'email' => $data['email'],
71-
'password' => Hash::make($data['password'] ?? Str::random(20)),
61+
'password' => Hash::make($data['password'] ?? Str::random(30)),
7262
]);
7363
}
7464
}

stubs/Identities/Models/Identity.stub

+16-12
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
namespace App;
44

55
use Illuminate\Database\Eloquent\Model;
6-
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use Oneofftech\Identities\Facades\Identity as IdentityFacade;
88

99
class Identity extends Model
1010
{
11-
12-
/**
13-
* @var array
14-
*/
15-
protected $casts = [
16-
'expires_at' => 'datetime',
17-
'registration' => 'bool',
18-
];
19-
2011
/**
2112
* @var array
2213
*/
@@ -39,11 +30,24 @@ class Identity extends Model
3930
'refresh_token',
4031
'expires_at',
4132
];
42-
33+
4334
/**
35+
* Get the attributes that should be cast.
4436
*
37+
* @return array<string, string>
38+
*/
39+
protected function casts(): array
40+
{
41+
return [
42+
'expires_at' => 'datetime',
43+
'registration' => 'bool',
44+
];
45+
}
46+
47+
/**
48+
* The user to whom this identity belongs.
4549
*/
46-
public function user()
50+
public function user(): BelongsTo
4751
{
4852
return $this->belongsTo(IdentityFacade::userModel());
4953
}

stubs/migrations/2020_08_09_115707_create_identities_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateIdentitiesTable extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -35,4 +35,4 @@ public function down()
3535
{
3636
Schema::dropIfExists('identities');
3737
}
38-
}
38+
};

tests/Fixtures/Http/Controllers/Controller.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,4 @@
22

33
namespace Tests\Fixtures\Http\Controllers;
44

5-
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6-
use Illuminate\Foundation\Bus\DispatchesJobs;
7-
use Illuminate\Foundation\Validation\ValidatesRequests;
8-
use Illuminate\Routing\Controller as BaseController;
9-
10-
class Controller extends BaseController
11-
{
12-
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
13-
}
5+
abstract class Controller {}

tests/Fixtures/Http/Controllers/Identities/Auth/ConnectController.php

-10
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ class ConnectController extends Controller
2323

2424
use ConnectUserIdentity;
2525

26-
/**
27-
* Create a new controller instance.
28-
*
29-
* @return void
30-
*/
31-
public function __construct()
32-
{
33-
$this->middleware('auth');
34-
}
35-
3626
/**
3727
* Get a validator for an incoming connection request.
3828
*

tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php

-10
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,4 @@ class LoginController extends Controller
2020
*/
2121

2222
use AuthenticatesUsersWithIdentity;
23-
24-
/**
25-
* Create a new controller instance.
26-
*
27-
* @return void
28-
*/
29-
public function __construct()
30-
{
31-
$this->middleware('guest');
32-
}
3323
}

tests/Fixtures/Http/Controllers/Identities/Auth/RegisterController.php

-10
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ class RegisterController extends Controller
2424

2525
use RegistersUsersWithIdentity;
2626

27-
/**
28-
* Create a new controller instance.
29-
*
30-
* @return void
31-
*/
32-
public function __construct()
33-
{
34-
$this->middleware('guest');
35-
}
36-
3727
/**
3828
* Get a validator for an incoming registration request.
3929
*

0 commit comments

Comments
 (0)