Difference between revisions of "User:Saul/feathers"
m (→Client Authentication) |
(→Authentication) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
feathers generate authentication | feathers generate authentication | ||
</source> | </source> | ||
+ | Authentication methods include: | ||
+ | <source> | ||
+ | Username + Password (Local) | ||
+ | Auth0 | ||
+ | Google | ||
+ | Facebook | ||
+ | GitHub | ||
+ | </source> | ||
+ | |||
== Services == | == Services == | ||
Services are a way to set up the REST API for a service like messages, users, channels, ect. | Services are a way to set up the REST API for a service like messages, users, channels, ect. | ||
Line 47: | Line 56: | ||
== Manipulating MongoDB Arrays == | == Manipulating MongoDB Arrays == | ||
https://docs.mongodb.com/manual/reference/operator/update-array/ | https://docs.mongodb.com/manual/reference/operator/update-array/ | ||
+ | |||
+ | == Curl == | ||
+ | Curl can be useful to test services as you create them. | ||
+ | === Create === | ||
+ | Create (POST /service) – creates a new document | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service" \ | ||
+ | -H "Content-Type: application/json" \ | ||
+ | --data-binary "{ 'email': 'feathers@example.com', 'password': 'secret' }"; | ||
+ | </source> | ||
+ | === Authenticate === | ||
+ | Create (POST /authentication) – authenticates a user | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/authetication" \ | ||
+ | -H "Content-Type: application/json" \ | ||
+ | --data-binary "{ 'strategy': 'local', 'email': 'feathers@example.com', 'password': 'secret' }"; | ||
+ | </source> | ||
+ | |||
+ | ==== Sending Auth Token ==== | ||
+ | To send the auth token with the command just add: | ||
+ | <source lang="bash"> | ||
+ | -H "Authorization: <your access token>" | ||
+ | </source> | ||
+ | For example: | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service/123abc" \ | ||
+ | -X PATCH \ | ||
+ | -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJ1c2VySWQiOiI1YjcwYzU3MjM5MTU0NDc5NzJjZGUzZDUiLCJpYXQiOjE1MzQxMTkxMDgsImV4cCI6MTUzNDIwNTUwOCwiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiYW5vbnltb3VzIiwianRpIjoiNWY3ZDY5YjktOWUxZS00NTgxLWFiMTItNjFkZmIxOTYxOGZkIn0.S_QHDjNf7DAp6-ZI3d_JpWgcMneBqfpkB2GgX_6eLwI" \ | ||
+ | -H "Content-Type: application/json" \ | ||
+ | --data-binary '{ "someProperty": "someValue" }' | ||
+ | </source> | ||
+ | |||
+ | === Get === | ||
+ | Get (GET /tasks/:id) – finds a single document by id | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service/123abc" | ||
+ | </source> | ||
+ | |||
+ | === Find === | ||
+ | Find (GET /tasks) – finds all the documents matching the query (optional), also supports sorting and pagination | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service" | ||
+ | </source> | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service?someProperty=someValue" \ | ||
+ | -GET | ||
+ | </source> | ||
+ | |||
+ | === Update === | ||
+ | Update (PUT /tasks/:id) – updates the whole document by overwriting it with provided one (missing fields will be cleared) | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service/123abc" \ | ||
+ | -X PUT \ | ||
+ | -H "Content-Type: application/json" \ | ||
+ | --data-binary '{ "someProperty": "someValue", "anotherProperty": "anotherValue" }' | ||
+ | </source> | ||
+ | |||
+ | === Patch === | ||
+ | Patch (PATCH /tasks/:id) – updates only the fields provided (missing fields will remain untouched) | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service/123abc" \ | ||
+ | -X PATCH \ | ||
+ | -H "Content-Type: application/json" \ | ||
+ | --data-binary '{ "someProperty": "someValue" }' | ||
+ | </source> | ||
+ | |||
+ | === Remove === | ||
+ | Remove (DELETE /tasks/:id) – removes the document | ||
+ | <source lang="bash"> | ||
+ | curl "http://localhost:3030/service/123abc" \ | ||
+ | -X DELETE | ||
+ | </source> | ||
+ | |||
= Troubleshooting = | = Troubleshooting = | ||
== Client Authentication == | == Client Authentication == | ||
I had an issue where the client authenticated and I received the authentication token but when I use a service it says that I'm not authenticated.<br> | I had an issue where the client authenticated and I received the authentication token but when I use a service it says that I'm not authenticated.<br> | ||
The solution was to ensure that the client setup the transport before the authentication. | The solution was to ensure that the client setup the transport before the authentication. |
Revision as of 05:44, 13 August 2018
Feathers is a lightweight framework for setting up REST API's and realtime events.
Contents
Install
CLI
Install the cli to get started with you feathers application quickly.
npm install -g @feathersjs/cli
App
To get feathers to generate the app run:
feathers generate app
Authentication
To generate a authentication method use:
feathers generate authentication
Authentication methods include:
Username + Password (Local)
Auth0
Google
Facebook
GitHub
Services
Services are a way to set up the REST API for a service like messages, users, channels, ect.
feathers generate service
Hooks
To generate a hook via the command line run:
feathers generate hook
Setup
TBC
Usage
Reverse Pagination
The best way that I have found to get the latest objects in the database is to use $sort like so:
app.service("messages").find({
query: {
$limit: 10,
$sort: {
createdAt: -1
}
}
}).then(res => {
const latestData = res.data.reverse();
};
Manipulating MongoDB Arrays
https://docs.mongodb.com/manual/reference/operator/update-array/
Curl
Curl can be useful to test services as you create them.
Create
Create (POST /service) – creates a new document
curl "http://localhost:3030/service" \
-H "Content-Type: application/json" \
--data-binary "{ 'email': 'feathers@example.com', 'password': 'secret' }";
Authenticate
Create (POST /authentication) – authenticates a user
curl "http://localhost:3030/authetication" \
-H "Content-Type: application/json" \
--data-binary "{ 'strategy': 'local', 'email': 'feathers@example.com', 'password': 'secret' }";
Sending Auth Token
To send the auth token with the command just add:
-H "Authorization: <your access token>"
For example:
curl "http://localhost:3030/service/123abc" \
-X PATCH \
-H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJ1c2VySWQiOiI1YjcwYzU3MjM5MTU0NDc5NzJjZGUzZDUiLCJpYXQiOjE1MzQxMTkxMDgsImV4cCI6MTUzNDIwNTUwOCwiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiYW5vbnltb3VzIiwianRpIjoiNWY3ZDY5YjktOWUxZS00NTgxLWFiMTItNjFkZmIxOTYxOGZkIn0.S_QHDjNf7DAp6-ZI3d_JpWgcMneBqfpkB2GgX_6eLwI" \
-H "Content-Type: application/json" \
--data-binary '{ "someProperty": "someValue" }'
Get
Get (GET /tasks/:id) – finds a single document by id
curl "http://localhost:3030/service/123abc"
Find
Find (GET /tasks) – finds all the documents matching the query (optional), also supports sorting and pagination
curl "http://localhost:3030/service"
curl "http://localhost:3030/service?someProperty=someValue" \
-GET
Update
Update (PUT /tasks/:id) – updates the whole document by overwriting it with provided one (missing fields will be cleared)
curl "http://localhost:3030/service/123abc" \
-X PUT \
-H "Content-Type: application/json" \
--data-binary '{ "someProperty": "someValue", "anotherProperty": "anotherValue" }'
Patch
Patch (PATCH /tasks/:id) – updates only the fields provided (missing fields will remain untouched)
curl "http://localhost:3030/service/123abc" \
-X PATCH \
-H "Content-Type: application/json" \
--data-binary '{ "someProperty": "someValue" }'
Remove
Remove (DELETE /tasks/:id) – removes the document
curl "http://localhost:3030/service/123abc" \
-X DELETE
Troubleshooting
Client Authentication
I had an issue where the client authenticated and I received the authentication token but when I use a service it says that I'm not authenticated.
The solution was to ensure that the client setup the transport before the authentication.