4.3.1 • Published 12 days ago

@kaname-png/plugin-api-jwt v4.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

Neko Plugins Logo

@kaname-png/plugin-api-jwt

Plugin for @sapphire/framework to overwrites the authentication strategy of the @sapphire/plugin-api plugin to JWT.

GitHub codecov npm (scoped) npm

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!

4.3.1

3 months ago

4.3.0

7 months ago

4.2.0

8 months ago

4.1.8

11 months ago

4.1.9

11 months ago

4.1.7

1 year ago

4.0.4

1 year ago

4.1.4

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

4.1.0

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago