This is intended to be a collection of misc things that I've had to do over and over again. It is small but will hopefully continue to grow.
Import the frontend code by using:
import { Polygon } from '@bucky24/toolbox/client';
Import the backend code by using:
const { Api } = require('@bucky24/toolbox/server');
Note components end with .jsx
. Make sure if you are using webpack that you handle this file extension accordingly.
The Polygon class contains methods for manipulating and getting data about polygons.
This method returns a boolean indicating if the given Point is inside the given Polygon
param | type | description |
---|---|---|
point | Point | The point to check against the polygon |
polygon | Polygon | The Polygon to check |
The Api class contains methods for calling api data
This method returns a Promise resolving to the result. This method expects to receive JSON as a response.
param | type | description |
---|---|---|
getBaseUrlFn | Function | This is run every call to determine what the base url (hostname) is |
method | String | This is expected to be an HTTP method, all uppercase |
url | String | The url to add to the base url |
data | Object | An optional object containing data to send with the API request |
headers | Object | An optional object containing headers to send |
The Auth class on the Frontend handles making authenticated requests with previously stored data
This method calls Api.callApi
with an Authentication
header mapping to a Bearer token. The parameters are the same as Api.callApi
The Auth class on the Backend handles registering and validating auth tokens.
This method can take in some data, create a JWT token, and send it both as a cookie, and as a token
field in the JSON response to the client.
param | type | description |
---|---|---|
res | Express Response | The response object for the api |
data | Object | The object to embed in JWT |
timeoutSec | Integer | The timestamp of how long the token should last. Defaults to 1 hour |
This method is an Express middleware. It verifies a valid token exists on either the session
cookie, or as part of the Authentication
header. It will place the extracted data on req.authData
.
The AuthContext (and AuthProvider) handle token storage and retrieval
variable | type | description |
---|---|---|
loading | Boolean | True while system is being initalized |
loggedIn | Boolean | True if a valid session token is found |
This method saves the token to localstorage
param | type | description |
---|---|---|
token | String | Token to store |
This method is basically the same as Auth.authenticatedFetch
.
This method provides functionality to handle clearing tokens.
The Router component wraps routes for react-router-dom
adding in the distinction between routes that need a logged in user and normal routes.
prop | type | description |
---|---|---|
routes | RouteData[] | List of routes that can be accessed without auth |
authedRoutes | RouteData[] | List of routes that can be accessed with auth |
loader | Component | Component to display while page is loaded |
loginRoute | String | Route to redirect to when the user tries to access an authenciated route without proper auth |
A point is a simple object that contains a numeric x and a numeric y.
The Polygon type is simply an array of Points.
A RouteData object contains the following properties
prop | type | description |
---|---|---|
path | String | Path to the route |
element | React Element | The element to render for the route |