Skip to content

Commit aeaa35c

Browse files
authored
fix: route list and auth scaffolding (#552)
* fix: route list and auth scaffolding * Fix styling * fix: docs and routes * fix: wi * Fix styling --------- Co-authored-by: binaryk <[email protected]>
1 parent cfe365a commit aeaa35c

11 files changed

+366
-48
lines changed

ROADMAP.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535

3636
### Features
3737

38-
- [ ] Adding a command that lists all Restify registered routes `php artisan restify:routes`
38+
- [x] Adding a command that lists all Restify registered routes `php artisan restify:routes`
3939
- [ ] UI for Restify
40-
- [ ] Support for Laravel 10
41-
- [ ] Custom namespace and base directory for repositories
40+
- [x] Support for Laravel 10
41+
- [x] Custom namespace and base directory for repositories
42+
- [ ] Deprecate `show` and use `view` as default policy method for `show` requests
43+
- [ ] Deprecate `store` and use `create` as default policy method for `store` requests so it's Laravel compatible

config/restify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
'user_verify_url' => env('FRONTEND_APP_URL').'/verify/{id}/{emailHash}',
4949

50-
'user_model' => \Illuminate\Foundation\Auth\User::class,
50+
'user_model' => "\App\Models\User",
5151
],
5252

5353
/*

docs-v2/content/en/auth/authentication.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ Laravel Restify has the support for a facile authentication with [Laravel Sanctu
99

1010
Now you can finally enjoy the auth setup (`register`, `login`, `forgot`, and `reset password`).
1111

12+
## Quick start
13+
14+
tl;dr:
15+
16+
If you run on Laravel 10 or higher, you can use this command that will do all the setup for you:
17+
18+
```shell script
19+
php artisan restify:setup-auth
20+
```
21+
22+
This command will:
23+
24+
- **ensures** that `Sanctum` is installed and configured as the authentication provider in the `config/restify.php` file
25+
- **appends** the `Route::restifyAuth();` line to the `routes/api.php` file to add the authentication routes
26+
1227
## Prerequisites
1328

1429
Migrate the `users`, `password_resets` table (they already exist into a fresh Laravel app).
@@ -116,7 +131,7 @@ Next, add the `auth:sanctum` middleware after the `api` middleware in your confi
116131

117132
## Login
118133

119-
Let's ensure the authentication is working correctly. Create a user in the DatabaseSeeder class:
134+
Let's ensure the authentication is working correctly. Create a user in the `DatabaseSeeder` class:
120135

121136
```php
122137
// DatabaseSeeder.php
@@ -165,6 +180,42 @@ So you should see the response like this:
165180
}
166181
```
167182

183+
### Authorization
184+
185+
We will discuss the authorization in more details here [Authorization](/auth/authorization). But for now let's see a simple example.
186+
187+
After a successful login, you will receive an authentication token. You should include this token as a `Bearer` token in the Authorization header for your subsequent API requests using [Postman](https://learning.postman.com/docs/sending-requests/authorization/#bearer-token), axios library, or cURL.
188+
189+
Here's an axios example for retrieving the user's profile with the generated token:
190+
191+
```js
192+
import axios from 'axios';
193+
194+
const token = '1|f7D1qkALtM9GKDkjREKpwMRKTZg2ZnFqDZTSe53k';
195+
196+
axios.get('http://restify-app.test/api/restify/profile', {
197+
headers: {
198+
'Authorization': `Bearer ${token}`,
199+
'Accept': 'application/json'
200+
}
201+
})
202+
.then(response => {
203+
console.log(response.data);
204+
})
205+
.catch(error => {
206+
console.error(error);
207+
});
208+
```
209+
210+
Here's a cURL example for retrieving the user's profile with the generated token:
211+
```bash
212+
curl -X GET "http://restify-app.test/api/restify/profile" \
213+
-H "Accept: application/json" \
214+
-H "Authorization: Bearer 1|f7D1qkALtM9GKDkjREKpwMRKTZg2ZnFqDZTSe53k"
215+
```
216+
217+
Replace `http://restify-app.test` with your actual domain and use the authentication token you received after logging in.
218+
168219
## Register
169220

170221
Let's see how to register a new user in the application. You can test the registration using Curl or Postman.

docs-v2/content/en/auth/authorization.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ Before diving into details about authorization, it is important for you to under
1515

1616
When you run a request (ie via Postman), it hits the Laravel application. Laravel will load every single Service Provider it has defined into `config/app.php` and [auto discovered ](https://laravel.com/docs/packages#package-discovery) providers as well.
1717

18-
Restify injects the `RestifyApplicationServiceProvider` in your `config/app.php` and it also has an auto discovered provider called `LaravelRestify\LaravelRestifyServiceProvider`.
18+
Restify injects the `RestifyApplicationServiceProvider` in your `config/app.php` and it also has an auto discovered provider called `\Binaryk\LaravelRestify\LaravelRestifyServiceProvider`.
1919

2020
- The `LaravelRestifyServiceProvider` is booted first. This will basically push the `RestifyInjector` middleware at the end of the middleware stack.
2121

2222
- Then, the `RestifyApplicationServiceProvider` is booted. This will define the gate, will load repositories and make the auth routes macro. You now have full control over this provider.
2323

2424
- The `RestifyInjector` will be handled. It will register all the routes.
2525

26-
- On each request, if the requested route is a Restify route, Laravel will handle other middlewares defined in the `restify.php` -> `middleware`.
26+
- On each request, if the requested route is a Restify route, Laravel will handle other middlewares defined in the `restify.php` -> `middleware`. Here is where you should have the `auth:sanctum` middleware to protect your API against unauthenticated users.
27+
28+
## Prerequisites
29+
30+
Before we dive into the details of authorization, we need to make sure that you have a basic understanding of how Laravel's authorization works. If you are not familiar with it, we highly recommend reading the [documentation](https://laravel.com/docs/authorization) before you move forward.
31+
32+
You may also visit the [Authentication/login](/auth/authentication#authorization) section to learn how to login and use the Bearer token.
2733

2834

2935
## View Restify

docs-v2/content/en/auth/profile.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@ category: Auth
55
position: 1
66
---
77

8-
## Sanctum middleware
8+
## Prerequisites
99

10-
To make sure you can get your profile just right, you should add the `auth:sanctum` middleware to the restify middleware config:
10+
Make sure you followed the [Authentication](/docs/auth/authentication) guide before, because one common mistake is that people do not add this middleware:
1111

1212
```php
1313
// config/restify.php
14-
1514
'middleware' => [
16-
'api',
15+
// ...
1716
'auth:sanctum',
18-
\Binaryk\LaravelRestify\Http\Middleware\DispatchRestifyStartingEvent::class,
19-
\Binaryk\LaravelRestify\Http\Middleware\AuthorizeRestify::class,
17+
// ...
2018
]
2119
```
2220

2321
## Get profile
2422

23+
Before retrieving the user's profile, you need to log in and obtain an authentication token. You can refer to the [login documentation](/auth/authentication#login) for details on how to authenticate a user. Make sure to include the `Bearer {$token}` in the `Authorization` header for the subsequent API requests, either using Postman or cURL.
24+
2525
When retrieving the user's profile, it is serialized by using the `UserRepository`.
2626

2727
```http request
2828
GET: /api/restify/profile
2929
```
3030

31+
Here's an example of a cURL request for retrieving the user's profile with a random token:
32+
33+
```bash
34+
curl -X GET "http://your-domain.com/api/restify/profile" \
35+
-H "Accept: application/json" \
36+
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9..."
37+
```
38+
39+
Replace `http://your-domain.com` with your actual domain and `eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...` with the authentication token you obtained after logging in.
40+
3141
This is what we have for a basic profile:
3242

3343
```json

src/Bootstrap/RoutesDefinition.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,51 @@ public function __invoke(string $uriKey = null)
2121
Route::get(
2222
$prefix.'/filters',
2323
\Binaryk\LaravelRestify\Http\Controllers\RepositoryFilterController::class
24-
);
24+
)->name('filters.index');
2525

2626
// Actions
2727
Route::get(
2828
$prefix.'/actions',
2929
\Binaryk\LaravelRestify\Http\Controllers\ListActionsController::class
30-
)->name('restify.actions.index');
30+
)->name('actions.index');
3131
Route::get(
3232
$prefix.'/{repositoryId}/actions',
3333
\Binaryk\LaravelRestify\Http\Controllers\ListRepositoryActionsController::class
34-
)->name('restify.actions.repository.index');
34+
)->name('actions.repository.index');
3535
Route::post(
3636
$prefix.'/action',
3737
\Binaryk\LaravelRestify\Http\Controllers\PerformActionController::class
38-
)->name('restify.actions.perform');
38+
)->name('actions.perform');
3939
Route::post(
4040
$prefix.'/actions',
4141
\Binaryk\LaravelRestify\Http\Controllers\PerformActionController::class
42-
); // alias to the previous route
42+
)->name('actions.performs'); // alias to the previous route
4343
Route::post(
4444
$prefix.'/{repositoryId}/action',
4545
\Binaryk\LaravelRestify\Http\Controllers\PerformRepositoryActionController::class
46-
)->name('restify.actions.repository.perform');
46+
)->name('actions.repository.perform');
4747
Route::post(
4848
$prefix.'/{repositoryId}/actions',
4949
\Binaryk\LaravelRestify\Http\Controllers\PerformRepositoryActionController::class
50-
); // alias to the previous route
50+
)->name('actions.repository.performs'); // alias to the previous route
5151

5252
// Getters
5353
Route::get(
5454
$prefix.'/getters',
5555
\Binaryk\LaravelRestify\Http\Controllers\ListGettersController::class
56-
)->name('restify.getters.index')->withoutMiddleware($this->excludedMiddleware);
56+
)->name('getters.index')->withoutMiddleware($this->excludedMiddleware);
5757
Route::get(
5858
$prefix.'/{repositoryId}/getters',
5959
\Binaryk\LaravelRestify\Http\Controllers\ListRepositoryGettersController::class
60-
)->name('restify.getters.repository.index')->withoutMiddleware($this->excludedMiddleware);
60+
)->name('getters.repository.index')->withoutMiddleware($this->excludedMiddleware);
6161
Route::get(
6262
$prefix.'/getters/{getter}',
6363
\Binaryk\LaravelRestify\Http\Controllers\PerformGetterController::class
64-
)->name('restify.getters.perform')->withoutMiddleware($this->excludedMiddleware);
64+
)->name('getters.perform')->withoutMiddleware($this->excludedMiddleware);
6565
Route::get(
6666
$prefix.'/{repositoryId}/getters/{getter}',
6767
\Binaryk\LaravelRestify\Http\Controllers\PerformRepositoryGetterController::class
68-
)->name('restify.getters.repository.perform')->withoutMiddleware($this->excludedMiddleware);
68+
)->name('getters.repository.perform')->withoutMiddleware($this->excludedMiddleware);
6969

7070
// API CRUD
7171
Route::get(
@@ -75,39 +75,39 @@ public function __invoke(string $uriKey = null)
7575
Route::post(
7676
$prefix.'',
7777
\Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController::class
78-
)->name('restify.store');
78+
)->name('store');
7979
Route::post(
8080
$prefix.'/bulk',
8181
\Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreBulkController::class
82-
)->name('restify.store.bulk');
82+
)->name('store.bulk');
8383
Route::post(
8484
$prefix.'/bulk/update',
8585
\Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateBulkController::class
86-
)->name('restify.update.bulk');
86+
)->name('update.bulk');
8787
Route::delete(
8888
$prefix.'/bulk/delete',
8989
\Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyBulkController::class
90-
)->name('restify.destroy.bulk');
90+
)->name('destroy.bulk');
9191
Route::get(
9292
$prefix.'/{repositoryId}',
9393
\Binaryk\LaravelRestify\Http\Controllers\RepositoryShowController::class
94-
)->name('restify.show')->withoutMiddleware($this->excludedMiddleware);
94+
)->name('show')->withoutMiddleware($this->excludedMiddleware);
9595
Route::patch(
9696
$prefix.'/{repositoryId}',
9797
\Binaryk\LaravelRestify\Http\Controllers\RepositoryPatchController::class
98-
)->name('restify.patch');
98+
)->name('patch');
9999
Route::put(
100100
$prefix.'/{repositoryId}',
101101
\Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController::class
102-
)->name('restify.put');
102+
)->name('put');
103103
Route::post(
104104
$prefix.'/{repositoryId}',
105105
\Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController::class
106-
)->name('restify.update');
106+
)->name('update');
107107
Route::delete(
108108
$prefix.'/{repositoryId}',
109109
\Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyController::class
110-
)->name('restify.destroy');
110+
)->name('destroy');
111111

112112
if ($uriKey) {
113113
return;
@@ -117,61 +117,61 @@ public function __invoke(string $uriKey = null)
117117
Route::delete(
118118
$prefix.'/{repositoryId}/field/{field}',
119119
\Binaryk\LaravelRestify\Http\Controllers\FieldDestroyController::class
120-
);
120+
)->name('field.destroy');
121121

122122
// Attach related repository id
123123
Route::post(
124124
$prefix.'/{repositoryId}/attach/{relatedRepository}',
125125
\Binaryk\LaravelRestify\Http\Controllers\RepositoryAttachController::class
126-
);
126+
)->name('attach');
127127
Route::post(
128128
$prefix.'/{repositoryId}/detach/{relatedRepository}',
129129
\Binaryk\LaravelRestify\Http\Controllers\RepositoryDetachController::class
130-
);
130+
)->name('detach');
131131
Route::post(
132132
$prefix.'/{repositoryId}/sync/{relatedRepository}',
133133
\Binaryk\LaravelRestify\Http\Controllers\RepositorySyncController::class
134-
);
134+
)->name('sync');
135135

136136
// Relatable
137137
Route::get(
138138
'/{parentRepository}/{parentRepositoryId}/{repository}',
139139
\Binaryk\LaravelRestify\Http\Controllers\RepositoryIndexController::class
140-
);
140+
)->name('relatable.index');
141141
Route::post(
142142
'/{parentRepository}/{parentRepositoryId}/{repository}',
143143
\Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController::class
144-
);
144+
)->name('relatable.store');
145145
Route::get(
146146
'/{parentRepository}/{parentRepositoryId}/{repository}/{repositoryId}',
147147
\Binaryk\LaravelRestify\Http\Controllers\RepositoryShowController::class
148-
);
148+
)->name('relatable.show');
149149
Route::post(
150150
'/{parentRepository}/{parentRepositoryId}/{repository}/{repositoryId}',
151151
\Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController::class
152-
);
152+
)->name('relatable.update');
153153
Route::put(
154154
'/{parentRepository}/{parentRepositoryId}/{repository}/{repositoryId}',
155155
\Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController::class
156-
);
156+
)->name('relatable.update');
157157
Route::delete(
158158
'/{parentRepository}/{parentRepositoryId}/{repository}/{repositoryId}',
159159
\Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyController::class
160-
);
160+
)->name('relatable.destroy');
161161
}
162162

163163
public function once(): void
164164
{
165-
Route::get('/search', GlobalSearchController::class);
165+
Route::get('/search', GlobalSearchController::class)->name('search');
166166

167-
Route::get('/profile', ProfileController::class);
168-
Route::put('/profile', ProfileUpdateController::class);
169-
Route::post('/profile', ProfileUpdateController::class);
167+
Route::get('/profile', ProfileController::class)->name('profile');
168+
Route::put('/profile', ProfileUpdateController::class)->name('profile.updatePut');
169+
Route::post('/profile', ProfileUpdateController::class)->name('profile.updatePost');
170170

171171
// RestifyJS
172172
Route::get('/restifyjs/setup', RestifyJsSetupController::class)->withoutMiddleware(
173173
RestifySanctumAuthenticate::class,
174-
);
174+
)->name('restifyjs.setup');
175175
}
176176

177177
public function withoutMiddleware(...$middleware): self

0 commit comments

Comments
 (0)