This is a wrapper around @keeer/kas-client with a user model, for the ease of use in a typical monolithic Node + Koa + database web application.
npm i @keeer/kas-helperSee example/index.js.
Creates a helper object. For accurate typings see the ts definition.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
| options.base | string | KAS base URL | |
| options.upsert | function | the callback to upsert a user, signature: async KASUser => any |
|
| options.get | function | the callback to get a user, signature: async (token: string) => KASUser |
|
| options.token | string | the service token for KAS, you need it if you want kiuid |
Returns an authentication middleware, will set the user at ctx.state.user.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
| kiuid | boolean | false | should we fetch the KEEER ID? |
Returns: KoaMiddleware
Returns an middleware to call the callback if KEEER ID is not set in or next otherwise. Note: this middleware asserts that the user is already logged in. Failure of this assumption will lead to an error.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
| unauthorizedCallback | function | the function to call if no KEEER ID is set, defaults to redirect to the set KEEER ID page |
Returns: KoaMiddleware
Returns an middleware to ensure that kiuid is set. Note: this middleware asserts that the user is already logged in. Failure of this assumption will lead to an error.
Returns: KoaMiddleware
Returns an middleware to call the callback if not logged in or next otherwise.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
| unauthorizedCallback | function | the function to call if unauthorized, defaults to set the status to 400 |
Returns: KoaMiddleware
Gets the user from a given context.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
| ctx | any | Koa context | ||
| kiuid | boolean | false | should we fetch the KEEER ID? |
Returns: KASUser | null, returns null if not logged in
Gets the user from a given token.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
| token | string | KAS account token | ||
| kiuid | boolean | false | should we fetch the KEEER ID? |
Returns: KASUser | null, returns null if not logged in
A user with a token.
Fetches kiuid and returns it.
Returns: string, kiuid
Let the user pay for something. This function will use KEEER ID to pay if it presents, or else it will use kiuid.
Parameters:
| Name | Type | Description |
|---|---|---|
| amount | number | Amount of centi-kredits to pay |
Returns: boolean, true for success, false for insufficient kredit
Throws: on other exceptions
Saves this user.