@kaname-png/plugin-api-jwt v4.3.1
@kaname-png/plugin-api-jwt
Plugin for @sapphire/framework to overwrites the authentication strategy of the @sapphire/plugin-api plugin to JWT.
Description
This plugin allows to override the authentication system of the @sapphire/plugin-api plugin for @sapphire/framework allowing a Bearer JWT based system.
This plugin does not change the behavior of the @sapphire/plugin-api plugin, so after installing the plugin you can continue to use the @sapphire/plugin-api plugin as you always have.
Features
- Fully ready for TypeScript!
- Includes ESM ready entrypoint
- Easy to use
Installation
@kaname-png/plugin-api-jwt
depends on the following packages. Be sure to install these along with this package!
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @kaname-png/plugin-api-jwt @sapphire/framework jwt-service @sapphire/plugin-api
Usage
JavaScript
In your main or setup file, register the plugin:
require('@kaname-png/plugin-api-jwt/register');
require('@sapphire/plugin-api/register');
Once the plugin is registered, we have to configure some options.
async function main() {
const client = new SapphireClient({
api: {
auth: {
id: 'xxx' /** client oauth id **/,
secret: 'xxx' /** client oauth secret **/,
redirect: 'https://kanama.moment/oauth' /** client oauth redirect **/,
strategy:
'jwt' /** Set the strategy to jwt if you want to use the jwt strategy for authentication or cookie if you want to use the default strategy of the @sapphire/plugin-api plugin. **/
}
}
});
await client.login();
}
void main();
TypeScript
In your main or setup file, register the plugin:
import '@kaname-png/plugin-api-jwt/register';
import '@sapphire/plugin-api/register';
Once the plugin is registered, we have to configure some options.
async function main() {
const client = new SapphireClient({
api: {
auth: {
id: 'xxx' /** client oauth id **/,
secret: 'xxx' /** client oauth secret **/,
redirect: 'https://kanama.moment/oauth' /** client oauth redirect **/,
strategy:
'jwt' /** Set the strategy to jwt if you want to use the jwt strategy for authentication or cookie if you want to use the default strategy of the @sapphire/plugin-api plugin. **/
}
}
});
await client.login();
}
void main();
How to use
Now, when you log in you will get a response like this, where the authentication token is attached.
Remember that the authentication token must be in the authorization
header with the value: Bearer [ token here ]
.
{
"data": {
"user": {
"id": "858367536240394259",
"username": "kaname-png",
"avatar": "28f2ec4eec159df460dc9b58f2a80318",
"discriminator": "1751",
"public_flags": 0,
"flags": 0,
"banner": null,
"banner_color": null,
"accent_color": null,
"verified": true
}
},
"token": "eyJhbGciOiJIUzI1NiJ9.XXXXX"
}
You can get the token information on a route, middleware, etc. in the following way:
import { ApiRequest, ApiResponse, methods, Route } from '@sapphire/plugin-api';
export class UserRoute extends Route {
constructor(context, options) {
super(context, {
...options,
route: ''
});
}
[methods.GET](_request: ApiRequest, response: ApiResponse) {
const tokenIfo = _request.auth;
response.json({ toke_info: tokenIfo });
}
}
It is important to remember that if the authorization token is invalid, then the _request.auth
variable will be null.
And as mentioned in the description, this plugin does not change the way @sapphire/plugin-api plugin is used, so you can follow the @sapphire/plugin-api plugin documentation.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
12 days ago
3 months ago
3 months ago
8 months ago
7 months ago
5 months ago
8 months ago
6 months ago
6 months ago
6 months ago
7 months ago
8 months ago
7 months ago
8 months ago
7 months ago
6 months ago
7 months ago
8 months ago
7 months ago
6 months ago
8 months ago
7 months ago
8 months ago
6 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
1 year ago
1 year ago
12 months ago
11 months ago
11 months ago
11 months ago
12 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago