@@ -23,21 +23,21 @@ the [https://github.com/Level51/silverstripe-jwt-utils/](JWTUtils) module.
23
23
24
24
_ app/\_ config/api.yml_
25
25
26
- ```
26
+ ``` yml
27
27
Level51\JWTUtils\JWTUtils :
28
28
secret : ' replace-this-with-a-jwt-secret-for-jwt'
29
29
lifetime_in_days : 365
30
30
renew_threshold_in_minutes : 60
31
31
` ` `
32
32
33
- Next step is to setup the routing for the API. You can modify the name of the
34
- routes as required for the project. At the very least you would have a
35
- project-specific end point which would subclass the ` ApiController ` for example,
33
+ Next step is to setup the routing for the API. You can modify the name of the
34
+ routes as required for the project. At the very least you would have a
35
+ project-specific end point which would subclass the ` ApiController` for example,
36
36
` MyProjectsApi` .
37
37
38
38
_app/\_config/routes.yml_
39
39
40
- ```
40
+ ` ` ` yml
41
41
SilverStripe\C ontrol\D irector:
42
42
rules:
43
43
'api/v1/auth/$Action': 'FullscreenInteractive\R estful\C ontrollers\A uthController'
@@ -50,7 +50,7 @@ list of all projects, logged in ADMIN users can `POST api/v1/projects/create`
50
50
51
51
_app/src/Project.php_
52
52
53
- ```
53
+ ` ` ` php
54
54
<?php
55
55
56
56
use FullscreenInteractive\R estful\I nterfaces\A piReadable;
@@ -63,24 +63,24 @@ class Project extends DataObject implements ApiReadable
63
63
'Title' => 'Varchar(100)',
64
64
'Date' => 'DBDate'
65
65
];
66
-
67
- private static $has_one = [
68
- 'Author' => Member::class
69
- ];
70
-
66
+
67
+ private static $has_one = [
68
+ 'Author' => Member::class
69
+ ];
70
+
71
71
public function toApi(): array
72
72
{
73
- return [
74
- 'title' => $this->Title,
75
- 'date' => $this->dbObject('Date')->getTimestamp()
76
- ];
77
- }
73
+ return [
74
+ 'title' => $this->Title,
75
+ 'date' => $this->dbObject('Date')->getTimestamp()
76
+ ];
77
+ }
78
78
}
79
79
` ` `
80
80
81
81
_app/src/MyProjectsApi.php_
82
82
83
- ```
83
+ ` ` ` php
84
84
<?php
85
85
86
86
class MyProjectsApi extends FullscreenInteractive\R estful\C ontrollers\A piController
@@ -101,7 +101,7 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
101
101
public function createProject()
102
102
{
103
103
$this->ensurePOST();
104
-
104
+
105
105
$member = $this->ensureUserLoggedIn([
106
106
'ADMIN'
107
107
]);
@@ -116,7 +116,7 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
116
116
$project = new Project();
117
117
$project->Title = $title;
118
118
$project->Date = $date;
119
- $project->AuthorID = $member->ID;
119
+ $project->AuthorID = $member->ID;
120
120
$project->write();
121
121
122
122
return $this->returnJSON([
@@ -127,7 +127,7 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
127
127
public function deleteProject()
128
128
{
129
129
$this->ensurePOST();
130
-
130
+
131
131
$member = $this->ensureUserLoggedIn([
132
132
'ADMIN'
133
133
]);
@@ -145,10 +145,10 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
145
145
]);
146
146
}
147
147
148
- if ($project->canDelete($member)) {
149
- $project->delete();
150
- }
151
-
148
+ if ($project->canDelete($member)) {
149
+ $project->delete();
150
+ }
151
+
152
152
return $this->success();
153
153
}
154
154
}
@@ -161,7 +161,7 @@ receive a token the user must first exchange their username / password over
161
161
basic authenication by making a `POST` request with the credentials. Usually
162
162
this is some form of javascript request e.g
163
163
164
- ```
164
+ ` ` ` js
165
165
fetch('/api/v1/auth/token', {
166
166
method: "POST",
167
167
headers: {
@@ -177,23 +177,23 @@ The response from that request with either be an error code (> 200) or if user
177
177
and password is correct, a 200 response containing the JWT. The token and
178
178
related meta data can be saved securely client side for reuse.
179
179
180
- ```
180
+ ` ` ` js
181
181
{
182
- "token": "eyJ0eXAiOiJKV1QiL...",
183
- "member": {
184
- "id": 1,
185
-
186
- "firstName": "Julian",
187
- "surname": "Scheuchenzuber"
188
- }
182
+ "token": "eyJ0eXAiOiJKV1QiL...",
183
+ "member": {
184
+ "id": 1,
185
+
186
+ "firstName": "Julian",
187
+ "surname": "Scheuchenzuber"
188
+ }
189
189
}
190
190
` ` `
191
191
192
192
If a user's token is invalid, or expired a *401* error will be returned. To
193
193
validate a users token use the `verify` endpoint - this will check the token and
194
194
renew the token if required.
195
195
196
- ```
196
+ ` ` ` js
197
197
fetch('/api/v1/auth/verify', {
198
198
method: "GET",
199
199
headers: {
@@ -206,7 +206,7 @@ fetch('/api/v1/auth/verify', {
206
206
207
207
The token can then be used to sign API calls as the `Bearer` header.
208
208
209
- ```
209
+ ` ` ` js
210
210
fetch('/api/v1/projects/createProject', {
211
211
method: "POST",
212
212
headers: {
@@ -218,6 +218,23 @@ fetch('/api/v1/projects/createProject', {
218
218
})
219
219
` ` `
220
220
221
+ # # UUIDs
222
+
223
+ https://stackoverflow.com/questions/56576985/is-it-a-bad-practice-to-expose-the-database-id-to-the-client-in-your-rest-api/56577271
224
+
225
+ When designing an API you may wish to avoid exposing your internal ID's to
226
+ in responses.
227
+
228
+ To add a UUID field to your object add the following extension to your model
229
+
230
+ ` ` ` php
231
+ private static $extensions = [
232
+ UuidableExtension::class
233
+ ];
234
+ ` ` `
235
+
236
+ A UUID will be generated on an objects `onBeforeWrite()` .
237
+
221
238
# # API Documentation
222
239
223
240
Todo but it's not massive. See `ApiController` for now.
0 commit comments